mySql:count列中具有相同数据的行数

前端之家收集整理的这篇文章主要介绍了mySql:count列中具有相同数据的行数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我试图选择表中的所有内容,并计算表中具有相同数据的行数.

  1. SELECT *,COUNT(thedate) daycount FROM `table` ORDER BY thedate DESC

我希望有一个查询输出与该日期相关的日期和行数,循环输出将是这样的:

Jan 1,2000 (2 rows)
col1,col2,col3,col4
col1,col4

Jan 1,2000 (3 rows)
col1,2000 (6 rows)
col1,col4

等等…

这有意义吗?

最佳答案
如果您有一个如下所示的表:

  1. CREATE TABLE yourtable
  2. (
  3. datefield DATETIME,col1 VARCHAR(20),col2 INT NOT NULL,col3 TINYINT NOT NULL,col4 CHAR(5)
  4. );

并且您希望每个给定日期重复col1 .. col4的计数,您将运行此查询

  1. SELECT
  2. COUNT(datefield) datefield_count,LEFT(all_fields,10) datefield,SUBSTR(all_fields,11) all_other_fields
  3. FROM
  4. (
  5. SELECT
  6. DATE(datefield) datefield,CONCAT(DATE(datefield),'|',COALESCE(col1,'< NULL >'),COALESCE(col2,COALESCE(col3,COALESCE(col4,'|') all_fields
  7. FROM
  8. yourtable
  9. ) A
  10. GROUP BY all_fields;

以下是一些示例数据和查询结果:

  1. MysqL> DROP TABLE IF EXISTS yourtable;
  2. Query OK,0 rows affected (0.04 sec)
  3. MysqL> CREATE TABLE yourtable
  4. -> (
  5. -> datefield DATETIME,-> col1 VARCHAR(20),-> col2 INT,-> col3 TINYINT,-> col4 CHAR(5)
  6. -> );
  7. Query OK,0 rows affected (0.11 sec)
  8. MysqL> INSERT INTO yourtable VALUES
  9. -> (DATE(NOW() - INTERVAL 1 DAY),'rolando',4,3,'angel'),-> (DATE(NOW() - INTERVAL 1 DAY),NULL,-> (DATE(NOW() - INTERVAL 2 DAY),2,'edwards'),-> (DATE(NOW() - INTERVAL 3 DAY),5,'pamela','angel')
  10. -> ;
  11. Query OK,22 rows affected,3 warnings (0.03 sec)
  12. Records: 22 Duplicates: 0 Warnings: 3
  13. MysqL> SELECT * FROM yourtable;
  14. +---------------------+---------+------+------+-------+
  15. | datefield | col1 | col2 | col3 | col4 |
  16. +---------------------+---------+------+------+-------+
  17. | 2011-06-30 00:00:00 | rolando | 4 | 3 | angel |
  18. | 2011-06-30 00:00:00 | rolando | 4 | 3 | angel |
  19. | 2011-06-30 00:00:00 | rolando | 4 | 3 | angel |
  20. | 2011-06-30 00:00:00 | rolando | 4 | NULL | angel |
  21. | 2011-06-30 00:00:00 | rolando | 4 | NULL | angel |
  22. | 2011-06-29 00:00:00 | rolando | 4 | 2 | angel |
  23. | 2011-06-29 00:00:00 | rolando | 4 | 2 | angel |
  24. | 2011-06-29 00:00:00 | rolando | 4 | 2 | angel |
  25. | 2011-06-29 00:00:00 | rolando | 4 | 2 | angel |
  26. | 2011-06-29 00:00:00 | rolando | 4 | NULL | edwar |
  27. | 2011-06-29 00:00:00 | rolando | 4 | NULL | angel |
  28. | 2011-06-28 00:00:00 | rolando | 5 | 2 | angel |
  29. | 2011-06-28 00:00:00 | rolando | 5 | 2 | angel |
  30. | 2011-06-28 00:00:00 | rolando | 4 | 2 | angel |
  31. | 2011-06-28 00:00:00 | pamela | 4 | 2 | angel |
  32. | 2011-06-28 00:00:00 | pamela | 4 | NULL | edwar |
  33. | 2011-06-28 00:00:00 | pamela | 5 | 2 | angel |
  34. | 2011-06-28 00:00:00 | pamela | 5 | 2 | angel |
  35. | 2011-06-28 00:00:00 | rolando | 4 | 2 | angel |
  36. | 2011-06-28 00:00:00 | rolando | 4 | 2 | angel |
  37. | 2011-06-28 00:00:00 | rolando | 4 | NULL | edwar |
  38. | 2011-06-28 00:00:00 | rolando | 4 | NULL | angel |
  39. +---------------------+---------+------+------+-------+
  40. 22 rows in set (0.00 sec)
  41. MysqL> SELECT
  42. -> COUNT(datefield) datefield_count,-> LEFT(all_fields,-> SUBSTR(all_fields,11) all_other_fields
  43. -> FROM
  44. -> (
  45. -> SELECT
  46. -> DATE(datefield) datefield,-> CONCAT(DATE(datefield),-> COALESCE(col1,-> COALESCE(col2,-> COALESCE(col3,-> COALESCE(col4,'|') all_fields
  47. -> FROM
  48. -> yourtable
  49. -> ) A
  50. -> GROUP BY all_fields;
  51. +-----------------+------------+----------------------------+
  52. | datefield_count | datefield | all_other_fields |
  53. +-----------------+------------+----------------------------+
  54. | 1 | 2011-06-28 | |pamela|4|2|angel| |
  55. | 1 | 2011-06-28 | |pamela|4|< NULL >|edwar| |
  56. | 2 | 2011-06-28 | |pamela|5|2|angel| |
  57. | 3 | 2011-06-28 | |rolando|4|2|angel| |
  58. | 1 | 2011-06-28 | |rolando|4|< NULL >|angel| |
  59. | 1 | 2011-06-28 | |rolando|4|< NULL >|edwar| |
  60. | 2 | 2011-06-28 | |rolando|5|2|angel| |
  61. | 4 | 2011-06-29 | |rolando|4|2|angel| |
  62. | 1 | 2011-06-29 | |rolando|4|< NULL >|angel| |
  63. | 1 | 2011-06-29 | |rolando|4|< NULL >|edwar| |
  64. | 3 | 2011-06-30 | |rolando|4|3|angel| |
  65. | 2 | 2011-06-30 | |rolando|4|< NULL >|angel| |
  66. +-----------------+------------+----------------------------+
  67. 12 rows in set (0.00 sec)
  68. MysqL>

我会留给你富有想象力的创造力来循环阅读和打印

> datefield
> datefield_count
>打印all_other_fields’datefield_count’次

试试看 !!!

猜你在找的MySQL相关文章