如何重写这个隐式连接到显式连接的位置

时间:2011-05-05 11:12:12

标签: sql join

 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%'

我在一个问题中遇到了这个SQL,但无法提供答案,因为我拒绝写where join个查询。

这个隐式连接是否给出了预期的(短)结果,或者这是否给出了交叉连接以及为什么?
如何使用显式连接重写它,而不添加新条件,因此不匹配(user.id = markers.user_id)等。

1 个答案:

答案 0 :(得分:3)

这是一个交叉联接。尝试这样的事情(请注意,我认为标记表和图像标记表有对用户表的引用。这可能不是这种情况!)

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%')

我不确定'明确加入'是什么意思,但我的猜测是:

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%'
相关问题