mysql查询未返回所需结果
我有三张桌子,我正试图从中获取结果,但结果却是空手而归。我的问题是:mysql查询未返回所需结果,mysql,sql,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.use
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
这不会返回任何结果,尽管肯定有符合此条件的记录(尽管显然不是我写的那样)。您使用了COUNT(o.order\u id)
而没有使用分组依据
。这导致了问题。如果没有使用分组依据
则无法执行此查询
从多个表中提取数据时,需要使用联接。您的查询应该是:
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
如果没有表模式和示例数据,就无法提供太多帮助。