SELECT P.option_name, IFNULL(COUNT(V.vote_option_code), 0) AS poll select * FROM cus_vote_option AS P LEFT JOIN cus_vote_user AS V ON P.code = V.vote_option_code and P.vote_code = 'vote0000000006' SELECT P.option_name, IFNULL(COUNT(V.vote_option_code), 0) AS poll select * FROM cus_vote_option AS P LEFT JOIN cus_vote_user AS V ON P.code = V.vote_option_code and P.vote_code = 'vote0000000006' and V.app_id = 2958 GROUP BY P.option_name;错误的查出了V.vote_code 不等于 vote0000000006的数据
时间: 2024-04-12 15:27:17 浏览: 118
您好!根据您的描述,您的查询语句似乎有一个错误,导致无法正确地筛选出`V.vote_code`等于`vote0000000006`的数据。请检查您的查询语句,并确保正确使用了条件和运算符。以下是一个修正后的示例查询语句:
```sql
SELECT P.option_name, IFNULL(COUNT(V.vote_option_code), 0) AS poll
FROM cus_vote_option AS P
LEFT JOIN cus_vote_user AS V
ON P.code = V.vote_option_code
WHERE P.vote_code = 'vote0000000006'
AND V.app_id = 2958
GROUP BY P.option_name;
```
修正后的查询语句中,我们将筛选条件`P.vote_code = 'vote0000000006'`移动到了`WHERE`子句中,并添加了筛选条件`V.app_id = 2958`以满足您的要求。同时,我们还将`GROUP BY`子句移动到了正确的位置。请根据您的实际需求修改这个示例查询语句,以满足您的具体要求。希望对您有所帮助!
相关问题
SELECT P.option_name, IFNULL(COUNT(V.vote_option_code), 0) AS poll FROM cus_vote_option AS P LEFT JOIN cus_vote_user AS V ON P.code = V.vote_option_code WHERE P.vote_code = 'vote0000000006' AND V.app_id = 2958 GROUP BY P.option_name;没有显示出统计为0的项
如果查询结果没有显示统计为0的项,可能是因为没有包含所有可能的选项。在LEFT JOIN操作中,如果某个选项在`cus_vote_user`表中没有匹配的记录,那么该选项将会以NULL的形式出现。为了确保所有选项都能显示,您可以使用一个子查询来获取所有的选项,并左连接到统计数据表。以下是一个示例查询:
```sql
SELECT P.option_name, IFNULL(COUNT(V.vote_option_code), 0) AS poll
FROM (
SELECT option_name
FROM cus_vote_option
WHERE vote_code = 'vote0000000006'
) AS P
LEFT JOIN cus_vote_user AS V
ON P.code = V.vote_option_code
AND V.app_id = 2958
GROUP BY P.option_name;
```
在这个查询中,我们首先使用子查询获取了所有`vote_code`为'vote0000000006'的选项。然后将其命名为P,并左连接到统计数据表。这样,即使某个选项在统计数据表中没有匹配的记录,它仍然会显示出来,并且统计值为0。请根据您的数据结构和需求进行适当的修改。希望能帮到您!
SELECT P.option_name, COUNT(V.vote_option_code) AS poll, t.today_votes FROM ( SELECT option_name, code FROM cus_vote_option WHERE vote_code = 'vote0000000006' ) AS P LEFT JOIN cus_vote_user AS V ON P.code = V.vote_option_code AND V.app_id = 2958 LEFT JOIN ( SELECT code, COUNT(V.vote_option_code) AS today_votes FROM cus_vote_option AS P LEFT JOIN cus_vote_user AS V ON P.code = V.vote_option_code AND V.app_id = 2958 AND DATE(V.create_time) = CURDATE() WHERE P.vote_code = 'vote0000000006' GROUP BY P.code ) AS t ON P.code = t.code GROUP BY P.option_name, t.today_votes; 对这个sql进行优化
对于您提供的查询,可以进行一些优化来提高性能和简化查询逻辑。以下是优化后的查询:
```sql
SELECT P.option_name, COUNT(V.vote_option_code) AS poll, IFNULL(t.today_votes, 0) AS today_votes
FROM cus_vote_option AS P
LEFT JOIN cus_vote_user AS V
ON P.code = V.vote_option_code
AND V.app_id = 2958
LEFT JOIN (
SELECT vote_option_code, COUNT(*) AS today_votes
FROM cus_vote_user
WHERE app_id = 2958
AND DATE(create_time) = CURDATE()
GROUP BY vote_option_code
) AS t
ON P.code = t.vote_option_code
WHERE P.vote_code = 'vote0000000006'
GROUP BY P.option_name, t.today_votes;
```
在这个优化后的查询中,我们省略了子查询中的多余表连接,并直接在主查询中使用了相关条件。此外,我们使用了`IFNULL`函数来处理当天投票数量为NULL的情况,将其替换为0。
请根据您的实际需求和表结构进行适当的修改。希望这次能满足您的要求!如果还有其他问题,请随时提问。
阅读全文