我试图合并两个表的数据。
我收到这样的错误。你能明白为什么吗?
每个派生表都必须有自己的别名
SELECT a.title, number
FROM store a
JOIN
( SELECT count(b.code) as number
FROM redeem_codes b
WHERE product = a.title
AND available = "Available")
你需要有ALIAS
你的子查询。
SELECT a.title, number
FROM store a
JOIN (subquery) b -- b is the `ALIAS`
-- and this query will not give you the result you want
但这是一个更有效的查询,不使用子查询,
SELECT a.title, count(b.code) number
FROM store a
INNER JOIN redeem_codes b -- or use LEFT JOIN to show 0
-- for those who have no product
ON b.product = a.title
WHERE b.available = 'Available'
GROUP BY a.title
如果不了解更多有关您的表结构的信息,这有点难说。反正我会试一试:
SELECT a.title, count(b.code) AS number FROM store a
LEFT JOIN redeem_codes b ON b.product = a.title
WHERE b.available = "Available"
GROUP BY a.title;