My-Sql JOIN两个表错误

时间:2012-10-13 09:04:30

标签: mysql sql join

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

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

  

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

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;