mysql查询没有返回所需的结果

时间:2013-05-30 04:16:51

标签: mysql sql

我有三张桌子,我试图从中得出结果,但我空手而归。这是我的疑问:

SELECT f . *, o . *, u.username, COUNT (o.order_id) as cnt 
FROM products f, orders o, users u 
WHERE f.product_id=o.product_id 
AND f.user_id = u.id 
AND (f.title LIKE '%hlt%' OR f.description LIKE '%hlt%' OR u.username LIKE '%hlt%') 
LIMIT 5

虽然确实有符合此标准的记录(但显然不是我写的),但是没有返回结果。

2 个答案:

答案 0 :(得分:0)

您使用COUNT (o.order_id)而不使用group BY。这导致了问题。您无法使用GROUP BY执行此查询

GROUP BY

答案 1 :(得分:0)

从多个表中提取数据时需要使用连接。您的查询应该是:

SELECT f . *, o . *, u.username, COUNT (o.order_id) as cnt 
FROM products f
JOIN orders o ON f.product_id=o.product_id
JOIN users u ON f.user_id = u.id 
WHERE (f.title LIKE '%hlt%' OR f.description LIKE '%hlt%' OR u.username LIKE '%hlt%') 
LIMIT 5
相关问题