使用带有计数的 MYSQL Distinct?
我希望在 MySQL 查询中同时使用 DISTINCT 和 COUNT,如下所示:
SELECT DISTINCT `data1` AS new_data, COUNT(new_data) FROM table_name WHERE `data2`='$data2' AND `data3` = '$data3'
目标是循环 DISTINCT 结果并对结果本身以及该结果的 COUNT 执行某些操作。
不幸的是,我对 MySQL 不太了解...
- 我不确定是否可以通过这种方式完成
- 是否有任何其他方法可以用来直接在 MySQL 中计算这一切,而无需多次数据库调用?
I'm looking to use both DISTINCT and COUNT in a MySQL query, something like this:
SELECT DISTINCT `data1` AS new_data, COUNT(new_data) FROM table_name WHERE `data2`='$data2' AND `data3` = '$data3'
The goal being to loop through the DISTINCT results and do something with both the result itself, as well as the COUNT for that result.
Unfortunately, I'm not too great with MySQL...
- I'm not sure if it can be done this way
- Is there any other approach that I could use to calculate this all directly in MySQL without multiple DB calls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您希望每个
data1
都有一个count
,其中行符合您的条件。正确的?按 data1 分组,就完成了:
I assume you want to have a
count
perdata1
where rows match your conditions. Right?Group by data1 and you're done:
我不确定你的问题...
你想做类似的事情吗?
I'm not sure to understand your question...
Do you want to do something like: