Mysql union all select问题
Posted
技术标签:
【中文标题】Mysql union all select问题【英文标题】:Mysql union all select issue 【发布时间】:2017-01-10 21:49:06 【问题描述】:我正试图找出我哪里出错了。我正在使用不同的 Where 语句从同一个表中加入多个选择语句,其中一个选择也是一个连接。我正在尝试使用 union all 但我似乎遗漏了一些东西,我无法弄清楚它是什么。任何帮助都会很棒。以下是我的代码:
SET @LASTQTR:=IF((QUARTER(CURDATE( ))-1) = 0, 4, QUARTER(CURDATE( ))-1);
SET @YR:=IF(@LASTQTR = 4, YEAR(NOW( ))-1, YEAR(NOW( )));
(SELECT COUNT(r.id) AS total, CONCAT(m1.first_name,' ', m1.last_name) AS fromName FROM Referrals AS r JOIN Members AS m1 ON m1.id=r.from_id WHERE QUARTER(rdate) = @LASTQTR AND YEAR(rdate) = @YR GROUP BY r.from_id)
UNION ALL
(SELECT COUNT(id) AS external FROM Referrals WHERE QUARTER(rdate) = @LASTQTR AND YEAR(rdate) = @YR AND rtype=1 GROUP BY from_id)
UNION ALL
(SELECT COUNT(id) AS internal FROM Referrals WHERE QUARTER(rdate) = @LASTQTR AND YEAR(rdate) = @YR AND rtype=2 GROUP BY from_id)
ORDER BY total DESC LIMIT 10;
我从这个查询中寻找的最终结果是在 while 循环中使用结果,如下所示:
while ( $re = $q8->fetch(PDO::FETCH_ASSOC))
echo'<tr>
<td style="width:40%;">'.$re['fromName'].'</td>
<td class="text-right">'.$re['external'].'</td>
<td class="text-right">'.$re['internal'].'</td>
<td class="text-right">'.$re['total'].'</td>
</tr>';
【问题讨论】:
【参考方案1】:所以我可能会在这种情况下帮助其他人,这就是我正在寻找的结果:
SELECT COUNT(r.id) AS total, CONCAT(m1.first_name,' ', m1.last_name) AS fromName,
(SELECT COUNT(id) FROM Referrals WHERE rtype=1 AND from_id=r.from_id GROUP BY fromName) AS external,
(SELECT COUNT(id) FROM Referrals WHERE rtype=2 AND from_id=r.from_id GROUP BY fromName) AS internal
FROM Referrals AS r JOIN Members AS m1 ON m1.id=r.from_id WHERE QUARTER(rdate) = @LASTQTR AND YEAR(rdate) = @YR GROUP BY fromName ORDER BY total DESC LIMIT 10
【讨论】:
【参考方案2】:对于联合,所有集合都应该具有相同的列。在您的情况下,第一个查询也有 fromName 。要么删除它,要么在其他查询中放置占位符
SET @LASTQTR:=IF((QUARTER(CURDATE( ))-1) = 0, 4, QUARTER(CURDATE( ))-1);
SET @YR:=IF(@LASTQTR = 4, YEAR(NOW( ))-1, YEAR(NOW( )));
(SELECT COUNT(r.id) AS total, CONCAT(m1.first_name,' ', m1.last_name) AS fromName FROM Referrals AS r JOIN Members AS m1 ON m1.id=r.from_id WHERE QUARTER(rdate) = @LASTQTR AND YEAR(rdate) = @YR GROUP BY r.from_id)
UNION ALL
(SELECT COUNT(id) AS external,'' FROM Referrals WHERE QUARTER(rdate) = @LASTQTR AND YEAR(rdate) = @YR AND rtype=1 GROUP BY from_id)
UNION ALL
(SELECT COUNT(id) AS internal,'' FROM Referrals WHERE QUARTER(rdate) = @LASTQTR AND YEAR(rdate) = @YR AND rtype=2 GROUP BY from_id)
ORDER BY total DESC LIMIT 10;
根据您的评论(尚未执行此操作)
(SELECT COUNT(r.id) AS total, CONCAT(m1.first_name,' ', m1.last_name) AS fromName,
(SELECT COUNT(id) FROM Referrals WHERE QUARTER(rdate) = @LASTQTR AND YEAR(rdate) = @YR AND rtype=1 GROUP BY from_id) AS external,
(SELECT COUNT(id) FROM Referrals WHERE QUARTER(rdate) = @LASTQTR AND YEAR(rdate) = @YR AND rtype=2 GROUP BY from_id) AS internal
FROM Referrals AS r JOIN Members AS m1 ON m1.id=r.from_id WHERE QUARTER(rdate) = @LASTQTR AND YEAR(rdate) = @YR GROUP BY r.from_id)
【讨论】:
使用上述方法的这个问题是,当循环遍历结果时,它将每个 select 语句作为一个新行放入 while 循环中。我认为使用 Union all 会返回每行所有三个选择的结果。以上是关于Mysql union all select问题的主要内容,如果未能解决你的问题,请参考以下文章