MySQL查询数据不等于列数据

时间:2018-05-28 14:43:25

标签: mysql

我在MySQL中使用这个结构

SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS `statistics`;
CREATE TABLE `statistics` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user` int(255) DEFAULT NULL,
  `machine_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `os_structure` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `os_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `os_version` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `processor_count` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `version` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `ip` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `country` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `country_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `date` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPRESSED;

到目前为止的查询是:

SELECT `os_name`, `os_structure`, COUNT(*) AS `count` FROM `statistics` GROUP BY `os_name`, `os_structure` ORDER BY `count` DESC;

我的问题是如何获取数据但不是来自ip结果的count列,因为我不是统计专家,所以我们欢迎任何改进。 以下是os_nameos_structurecount的示例,当我尝试分组时,真/假结果等于64位系统(对于true)或32位系统(对于假) ip结果超出预期我得了很多次windows 7 professional而不是我想要的。

Microsoft Windows 7 Professional    True    36
Microsoft Windows 10 Pro            True    26
Microsoft Windows 7 Ultimate        False   12
Microsoft Windows 7 Professional    False   11
Microsoft Windows 7 Ultimate        True    5
Microsoft Windows 7 Enterprise      False   1
Microsoft Windows 7 Professional N  True    1
Microsoft Windows 7 Ultimate K      False   1

1 个答案:

答案 0 :(得分:0)

也许是一个计数(不同)

例如

MariaDB [sandbox]> select status,username from users;
+--------+----------+
| status | username |
+--------+----------+
|     14 | John     |
|     13 | Jane     |
|     12 | Ali      |
|     11 | Bruce    |
|     10 | Martha   |
|      9 | Sidney   |
|      8 | charlie  |
|      7 | Elisa    |
|      6 | Samantha |
|      5 | Hannah   |
|      5 | Hannah   |
|      3 | Kevin    |
+--------+----------+
12 rows in set (0.00 sec)

MariaDB [sandbox]> select status,count(*), count(distinct username) from users group by status;
+--------+----------+--------------------------+
| status | count(*) | count(distinct username) |
+--------+----------+--------------------------+
|      3 |        1 |                        1 |
|      5 |        2 |                        1 |
|      6 |        1 |                        1 |
|      7 |        1 |                        1 |
|      8 |        1 |                        1 |
|      9 |        1 |                        1 |
|     10 |        1 |                        1 |
|     11 |        1 |                        1 |
|     12 |        1 |                        1 |
|     13 |        1 |                        1 |
|     14 |        1 |                        1 |
+--------+----------+--------------------------+
11 rows in set (0.00 sec)

注意状态5.