开发者

How to rewrite this implicit where join into explicit join

开发者 https://www.devze.com 2023-03-03 05:55 出处:网络
SELECT * FROM `users` u, `markers` m,`imagemarkers` im WHERE u.username LIKE \'%test%\' OR u.email LIKE \'%test%\'
 SELECT * FROM `users` u, `markers` m,`imagemarkers` im
 WHERE u.username LIKE '%test%'
  OR u.email LIKE '%test%'
  OR u.location LIKE '%test%'
  OR m.author LIKE '%test%'
  OR m.bike LIKE '%test%'
  OR m.title LIKE '%test%'

I came across this SQL in a question, but was unable to provide an answer, because I refuse to write where join querie开发者_开发技巧s.

Does this implicit join give the expected (short) result, or does this give a cross join and why?

How do I rewrite this using explicit joins, without adding new conditions, so no match on (user.id = markers.user_id) and such.


This is a cross join. Try something like this (Please note that I suppose markers table and imagemarkers table has a reference to the users table. This may well not be the case!)

SELECT * FROM `users` u, `markers` m,`imagemarkers` im
 WHERE m.user_id = i.id AND im.user_id = u.id
  AND (u.username LIKE '%test%'
  OR u.email LIKE '%test%'
  OR u.location LIKE '%test%'
  OR m.author LIKE '%test%'
  OR m.bike LIKE '%test%'
  OR m.title LIKE '%test%')

I'm not sure what you mean by 'explicit join' but my guess is:

SELECT * FROM `users` u 
 inner join `markers` m on (m.user_id = i.id) 
 inner join  `imagemarkers` im on (im.user_id = u.id) 
 WHERE u.username LIKE '%test%'
  OR u.email LIKE '%test%'
  OR u.location LIKE '%test%'
  OR m.author LIKE '%test%'
  OR m.bike LIKE '%test%'
  OR m.title LIKE '%test%'
0

精彩评论

暂无评论...
验证码 换一张
取 消