SELECT P.option_name, COUNT(V.vote_option_code) AS poll 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 GROUP BY P.option_name; 需要加上统计当天数量的列
时间: 2024-04-12 13:27:24 浏览: 99
如果您想要在结果中添加每个选项在当天的统计数量,您可以使用子查询的方式来实现。以下是一个示例查询:
```sql
SELECT P.option_name, COUNT(V.vote_option_code) AS poll, P.option_count
FROM (
SELECT option_name, code, COUNT(*) AS option_count
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;
```
在这个查询中,我们在子查询中添加了一个新的列`option_count`,该列用于统计每个选项的数量。然后,在主查询中,我们将`P.option_count`包含在结果集中,以显示每个选项在当天的统计数量。
请根据您的数据结构和需求进行适当的修改。希望这次能帮到您!如果还有其他问题,请随时提问。
相关问题
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的数据
您好!根据您的描述,您的查询语句似乎有一个错误,导致无法正确地筛选出`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, 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。
请根据您的实际需求和表结构进行适当的修改。希望这次能满足您的要求!如果还有其他问题,请随时提问。
阅读全文