-4

我试图合并两个表的数据。

我收到这样的错误。你能明白为什么吗?

每个派生表都必须有自己的别名

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")
4

2 回答 2

1

你需要有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
于 2012-10-13T09:05:48.823 回答
1

如果不了解更多有关您的表结构的信息,这有点难说。反正我会试一试:

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;
于 2012-10-13T09:14:08.027 回答