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)等。
答案 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%'