Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql联盟无法按预期工作_Mysql_Union - Fatal编程技术网

mysql联盟无法按预期工作

mysql联盟无法按预期工作,mysql,union,Mysql,Union,我有一个mysql查询: Select count(*) as cnt from page06 where dl = 1 and TYPE = 'mempage' union Select count(*) as cnt from page06 where err = 1 and TYPE = 'mempage' union Select count(*) as cnt from page06 where dl = 1 and err=1 and TYPE = 'mempage'

我有一个mysql查询:

Select count(*) as cnt from  page06 where dl = 1 and TYPE = 'mempage' union 
Select count(*) as cnt from  page06 where err = 1 and TYPE = 'mempage' union  
Select count(*) as cnt from  page06 where dl = 1 and err=1 and TYPE = 'mempage'
在大多数情况下,dl和err列为0,因此最后两个select将返回0。我发现这个查询只返回一个与我期望的不同的结果。我找到了这个解决方案:

Select count(*) as cnt,'s' as p from  page06 where dl = 1 and TYPE = 'mempage' union 
Select count(*),'d' as p  from  page06 where err = 1 and TYPE = 'mempage' union  
Select count(*),'f' as p   from  page06 where dl = 1 and err=1 and TYPE = 'mempage'
这个查询非常有效,但我已经用第一种格式编写了代码。我想知道为什么会发生这种情况,除了我所说的以外,还有什么解决办法吗? 我不想使用我的解决方案的原因是我有很多书面查询,我不想或甚至无法更改它们!我正在寻找的是一些可以设置为连接的选项,比如设置名称“latin1”

使用UNION ALL而不是UNION ALL

查看此帖子,了解有关差异的详细解释:


UNION将丢弃最终结果中的重复项。使用UNION ALL以确保返回所有行。谢谢,工作正常。很抱歉问这个不相关的问题,但是你知道学习mysql的好资源吗?
SELECT count(*) AS cnt FROM  page06 WHERE dl = 1 AND TYPE = 'mempage' UNION ALL
SELECT count(*) AS cnt FROM  page06 WHERE err = 1 AND TYPE = 'mempage' UNION ALL
SELECT count(*) AS cnt FROM  page06 WHERE dl = 1 AND err=1 AND TYPE = 'mempage'