php - 如何使用最佳方法获得一行中多个字段的不同计数

标签 php mysql

我的演示数据库中有 2 个表,其中我根据 2 个键连接它们,在这里我想计算匹配结果的不同值,

我的第一张 table

MariaDB [demos]> select * from main_info;
+------+------+-------+-------+----------+
| key1 | key2 | info1 | info2 | date     |
+------+------+-------+-------+----------+
|    1 |    1 |    15 |    90 | 20120501 |
|    1 |    2 |    14 |    92 | 20120601 |
|    1 |    3 |    15 |    82 | 20120801 |
|    2 |    1 |    17 |    90 | 20130302 |
|    2 |    2 |    16 |    88 | 20130601 |
+------+------+-------+-------+----------+
5 rows in set (0.00 sec)

还有我的第二张 table

MariaDB [demos]> select * from product1;
+------+------+--------+--------------+
| key1 | key2 | serial | product_data |
+------+------+--------+--------------+
|    1 |    1 |      0 | NaN          |
|    1 |    1 |      1 | NaN          |
|    1 |    1 |      2 | NaN          |
|    1 |    1 |      3 | NaN          |
|    1 |    2 |      0 | 12.556       |
|    1 |    2 |      1 | 13.335       |
|    1 |    3 |      1 | NaN          |
|    1 |    3 |      2 | 13.556       |
|    1 |    3 |      3 | 14.556       |
|    2 |    1 |      0 | 12.556       |
|    2 |    1 |      1 | 13.553       |
|    2 |    1 |      2 | NaN          |
+------+------+--------+--------------+
12 rows in set (0.00 sec)

因此字段序列的不同计数如下,其中序列不为零

MariaDB [demos]> select count(distinct a.key1,a.key2) as serial_count from main_info a,product1 b where a.key1=b.key1 and a.key2=b.key2 and b.serial !=0;
+--------------+
| serial_count |
+--------------+
|            4 |
+--------------+
1 row in set (0.00 sec)

字段product_data的不同计数如下,其中product_data不为NaN

MariaDB [demos]> select count(distinct a.key1,a.key2) as product_count from main_info a,product1 b where a.key1=b.key1 and a.key2=b.key2 and b.product_data !='NaN';
+---------------+
| product_count |
+---------------+
|             3 |
+---------------+
1 row in set (0.00 sec)

在我原来的应用程序中,我的表中有很多列,最后我想使用单个查询得到如下结果,目前我正在使用 PHP 进行多个查询,这需要很长时间,因为例如如果我有 100 列,我必须像上面一样执行函数 100 次,所以请有人指导我加快我的应用程序

+--------------+---------------+
| serial_count |product_count  |
+--------------+---------------+
|    4         |    3          |
+--------------+---------------+

以下是表格结构

DROP TABLE IF EXISTS `main_info`;
CREATE TABLE `main_info` (
  `key1` int(11) NOT NULL,
  `key2` int(11) NOT NULL,
  `info1` int(11) NOT NULL,
  `info2` int(11) NOT NULL,
  `date` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


LOCK TABLES `main_info` WRITE;
INSERT INTO `main_info` VALUES (1,1,15,90,20120501),(1,2,14,92,20120601),(1,3,15,82,20120801),(2,1,17,90,20130302),(2,2,16,88,20130601);
UNLOCK TABLES;


DROP TABLE IF EXISTS `product1`;
CREATE TABLE `product1` (
  `key1` int(11) NOT NULL,
  `key2` int(11) NOT NULL,
  `serial` int(11) NOT NULL,
  `product_data` varchar(1000) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


LOCK TABLES `product1` WRITE;
INSERT INTO `product1` VALUES (1,1,0,'NaN'),(1,1,1,'NaN'),(1,1,2,'NaN'),(1,1,3,'NaN'),(1,2,0,'12.556'),(1,2,1,'13.335'),(1,3,1,'NaN'),(1,3,2,'13.556'),(1,3,3,'14.556'),(2,1,0,'12.556'),(2,1,1,'13.553'),(2,1,2,'NaN');
UNLOCK TABLES;

如果我在终端上运行,我不明白为什么日期类型会改变

$ mysql -u root -p demos -e 'select key1,if(key1 !=0,key1,999.999) as `test1` from main_info'
Enter password: 
+------+-------+
| key1 | test1 |
+------+-------+
|    1 | 1.000 |
|    1 | 1.000 |
|    1 | 1.000 |
|    2 | 2.000 |
|    2 | 2.000 |
+------+-------+

如果IF条件为真,我希望它应该是整数

最佳答案

您可以使用条件聚合在单个查询中完成此操作:

select count(distinct if(b.product_data !='NaN',a.key1, null),  
                      if(b.product_data !='NaN',a.key2, null)) as product_count,
       count(distinct if(b.serial !=0,a.key1, null),  
                      if(b.serial !=0,a.key2, null)) as serial_count                     
from main_info a
inner join product1 b on a.key1=b.key1 and a.key2=b.key2

输出:

product_count   serial_count
-----------------------------
3               4

Demo here

编辑:归功于@Paul Spiegel

可以使用CONCAT简化查询:

select count(distinct if(b.product_data !='NaN', 
                         CONCAT(a.key1, ',', a.key2), 
                         null)) as product_count,
       count(distinct if(b.serial !=0, 
                         CONCAT(a.key1, ',', a.key2), 
                         null)) as serial_count                     
from main_info a
inner join product1 b on a.key1=b.key1 and a.key2=b.key2

关于php - 如何使用最佳方法获得一行中多个字段的不同计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35526601/

相关文章:

php - 如何使用 PHP 处理动态生成的图表并将结果作为电子邮件发送?

php - 在 foreach 循环内从 MySQL 中删除记录 - PHP

php - 为数据库表自动生成 CRUD UI 的应用程序

php mysql 从表循环行更新数组

c# - 将属性添加到关联

php - 如何在 PHP 中将日期和时间字符串转换为当前时区日期

javascript - 在 PHP 中更新图像的问题

php - php中fileatime和filectime的区别

php - Laravel 5.6 多重登录错误

mysql - 使用另一列中的值计算一列中的值的数量