我希望在MySQL查询中同时使用DISTINCT和COUNT,如下所示:
SELECT DISTINCT `data1` AS new_data, COUNT(new_data) FROM table_name WHERE `data2`='$data2' AND `data3` = '$data3'
目标是遍历DISTINCT结果并对结果本身以及该结果的COUNT执行某些操作。
不幸的是,我对MySQL不太好......
答案 0 :(得分:1)
我假设您希望count
每data1
行符合您的条件。对?
按数据分组1,您已完成:
SELECT `data1`, COUNT(*)
FROM table_name
WHERE `data2` = '$data2' AND `data3` = '$data3'
GROUP BY `data1`
答案 1 :(得分:1)
我不确定你的问题......
你想做点什么:
SELECT COUNT(DISTINCT `data1`) AS new_data FROM table_name
WHERE `data2`='$data2' AND `data3` = '$data3'