我正在尝试构建一个查询,该查询将采用一些默认值并将保持与表的连接。这只是为了在主查询(第二个)与任何行不匹配的情况下允许至少一个结果,因此可能返回空结果集as mentioned on my another question。
我的查询大致如下:
SELECT * FROM(
(SELECT 'test' as `column1`, 'test' as `column2`, 0 as c) t1
LEFT JOIN
(SELECT `column1`, `column2`, COUNT(*) as c
FROM `my_table`
WHERE `status` = 1
GROUP BY column1, column2) t2
)
它在phpmyadmin上显示一个mysql错误,如下所示:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't2 )' at line 7
请问查询有什么问题?
答案 0 :(得分:1)
我认为这是您想要的查询:
SELECT t1.column1, t2.column2, coalesce(t2.c, 0)
FROM (SELECT 'test' as `column1`, 'test' as `column2`, 0 as c
) t1 LEFT JOIN
(SELECT `column1`, `column2`, COUNT(*) as c
FROM `my_table`
WHERE `status` = 1
GROUP BY column1, column2
) t2
on t1.column1 = t2.column1 and t1.column2 = t2.column2;
您的查询不完整。例如,它没有on
子句。