SQL语法错误 - 嵌套查询

时间:2014-01-03 13:51:55

标签: mysql

SQL不是我最强的区域,我试图查询两个表从两个表中拉回行并同时依赖行。这是我尝试的代码。

SELECT author_name, tos_signed, date_signed
(
    SELECT COUNT(*) AS total_signed FROM Tos WHERE tos_signed = '1'
)
FROM Authors, Tos WHERE Authors.wordpress_id = Tos.wordpress_id;

3 个答案:

答案 0 :(得分:1)

对于语法错误,您在date_signed之后缺少逗号,为了计数,您可以尝试这个

SELECT author_name, tos_signed, date_signed,SUM(tos_signed = '1')  AS total_signed    
FROM Authors a
join  Tos t on 
a.wordpress_id = t.wordpress_id
GROUP BY your id col /* or the column you need to get the count for each group*/
;

答案 1 :(得分:0)

我想你需要这个 -

SELECT author_name, tos_signed, date_signed
(
    SELECT COUNT(*) AS total_signed FROM Tos a WHERE a.tos_signed = '1' and a.wordpress_id = Tos.wordpress_id
)
FROM Authors, Tos WHERE Authors.wordpress_id = Tos.wordpress_id;

答案 2 :(得分:-1)

此时每行都会返回相同的整数。 您希望在子选择中包含 where

SELECT author_name, 
       tos_signed, 
       date_signed,
       (
        SELECT COUNT(*) AS total_signed  FROM Tos WHERE tos_signed = '1' 
                                                  and  Authors.wordpress_id = Tos.wordpress_id;
       )
    FROM Authors;