我有一个表名作为用户(id,Name,Status,joined_date)。
id | Name | status | joined_date
-------------------------------------
1 | TDS | 0 | 2014-10-16
2 | TDS | 1 | 2014-10-11
3 | VAT | 0 | 2014-10-11
4 | IT | 0 | 2014-10-11
5 | IT | 1 | 2014-10-11
6 | TDS | 0 | 2014-10-16
7 | TDS | 1 | 2014-10-30
8 | VAT | 0 | 2014-11-01
9 | IT | 0 | 2014-10-07
10 | IT | 1 | 2014-10-11
在我的结果中,我需要计算当月的姓名和当月的姓名,状态为1。对于每个名称,两者都会产生相同的行。
Name | current_month | current_month_ststus_1
-----------------------------------------------------
VAT | 2 | 0
IT | 4 | 1
TDS | 4 | 2
我对最后几天的查询有点困惑。请尽可能帮助我。 提前致谢
答案 0 :(得分:0)
如果状态仅允许0,1,
你可以得到这样的结果。
select count(Name) as current_month,sum(status) as current_month_status_1 from table group by currentmonth
答案 1 :(得分:0)
SELECT
Name,
COUNT(Status) AS current_month,
SUM(status) AS current_month_status_1
FROM
Usertable
WHERE
MONTH(joined_date) = MONTH(NOW())
AND YEAR(joined_date) = YEAR(NOW())
GROUP BY
Name,
YEAR(joined_date),
MONTH(joined_date)
这假设状态只能是1或0。
答案 2 :(得分:0)
SELECT name,count(status)as current_month,sum(status)as current_month_status FROM
usertable
WHERE月(joined_date)= MONTH(CURDATE())GROUP BY名称
答案 3 :(得分:0)
选择category_id,(从category_images中选择count(id),其中delete_status ='1'和
id = o.id group by category_id)as delete1,
(从ategory_images中选择count(id),其中delete_status ='0',id = o.id group by
category_id)from category_images o
中的delete2我使用category_id你必须使用名称字段好吗我希望这可以解决你的问题