通过join和/或union将两个MySQL查询简化为一个

时间:2013-09-28 16:10:05

标签: php mysql join group-by union

我有两个问题:

(SELECT b904_Vasarlas.PrintedFigure as Image, 
    b904_Vasarlas.ProductColor as Color, 
    SUM(b904_Vasarlas.QTY) as QTY 
FROM `b904_Vasarlas` 
GROUP BY 1, 2) 
ORDER by 1, 2

产出良好:

Image;Color;QTY
"villamos","blue","5"
"villamos","pink","5"
"virag","blue","5"
"virag","pink","5"
"virag2","blue","5"
"virag2","pink","5"
"vitorlas","blue","5"
"vitorlas","pink","5"
"vonat","blue","5"
"vonat","pink","5"
"zaszlo","blue","5"
"zaszlo","pink","5"
"zsiraf","blue","15"
"zsiraf","pink","15"

(SELECT b904_Eladas.PrintedFigure as Image, 
    b904_Eladas.ProductColor as Color, 
    SUM(b904_Eladas.QTY) as QTY 
FROM `b904_Eladas` 
GROUP BY 1, 2) 
ORDER by 1, 2

产出良好:

Image;Color;QTY
"golya","blue","20"
"golya","pink","10"
"hajokormany","blue","20"
"hajokormany","pink","10"
"macska","blue","10"
"macska","pink","10"
"malac","blue","10"
"malac","pink","10"
"villamos","blue","3"
"villamos","pink","3"
"virag","blue","3"
"virag","pink","3"
"virag2","blue","4"
"vitorlas","blue","5"
"vitorlas","pink","5"
"vonat","blue","5"
"vonat","pink","5"
"zaszlo","blue","15"
"zaszlo","pink","5"
"zsiraf","blue","16"
"zsiraf","pink","11"

如何才能区分两个表的QTY列,只有一个mysql查询按图像和颜色列分组?


最后我使用了这个更长的代码,但我认为只用一个查询字符串就能解决问题更简单。

$Eladasok = array();
$ElLoop = 0;
$GyartasQuery = '(SELECT ' . $DBConnection[1] . '_Vasarlas.PrintedFigure as RaktarKep, ' . $DBConnection[1] . '_Vasarlas.ProductColor as RaktarSzin, SUM(' . $DBConnection[1] . '_Vasarlas.QTY) as RaktarQTY  FROM `' . $DBConnection[1] . '_Vasarlas` GROUP BY 1, 2) ORDER by 1, 2';
$EladasQuery = '(SELECT ' . $DBConnection[1] . '_Eladas.PrintedFigure as EladasKep, ' . $DBConnection[1] . '_Eladas.ProductColor as EladasSzin, SUM(' . $DBConnection[1] . '_Eladas.QTY) as EladasQTY  FROM `' . $DBConnection[1] . '_Eladas` GROUP BY 1, 2) ORDER by 1, 2';
$GyartasResult = mysql_query($GyartasQuery);
$EladasResult = mysql_query($EladasQuery);

while(false !== $EladasContent = mysql_fetch_assoc($EladasResult)) {                        
    $new_array[] = $EladasContent;
    $Eladasok[$new_array[$ElLoop][EladasKep] . '-' . $new_array[$ElLoop][EladasSzin]] = $EladasContent[EladasQTY];
    $ElLoop++;
}

while(false !== $GyartasContent = mysql_fetch_assoc($GyartasResult)) {
    if (($GyartasContent[RaktarQTY] - $Eladasok[$GyartasContent[RaktarKep] . '-' . $GyartasContent[RaktarSzin]]) <= 0) {
        $Cellcolour = ' bgcolor="#DDDDDD"';
        $CellHighlight = ' bgcolor="#FFBBBB"';
    } else {
        $Cellcolour = '';
        $CellHighlight = '';
    }

    echo'
    <tr><td align=center' . $CellHighlight . '>'.++$RoNo.'</td>
    <td' . $Cellcolour . '>' . $GyartasContent[RaktarKep] . ' - ' . $GyartasContent[RaktarSzin] .  '</td>
    <td' . $Cellcolour . '>' . intval($GyartasContent[RaktarQTY]) . '</td>
    <td' . $Cellcolour . '>' . intval($Eladasok[$GyartasContent[RaktarKep] . '-' . $GyartasContent[RaktarSzin]]) . '</td>
    <td' . $Cellcolour . '>' .  intval($GyartasContent[RaktarQTY] - $Eladasok[$GyartasContent[RaktarKep] . '-' . $GyartasContent[RaktarSzin]]). 
    '</td></tr>';
}
echo '</tr></table></div>';

3 个答案:

答案 0 :(得分:0)

database1的内容:

SELECT PrintedFigureProductColorQTY FROM b904_vasarlas

(Name, color, QTY)
"villamos","blue","5"
"villamos","pink","5"
"virág","blue","5"
"virág","pink","5"
"virág2","blue","5"
"virág2","pink","5"
"vitorlás","blue","5"
"vitorlás","pink","5"
"vonat","blue","5"
"vonat","pink","5"
"zsiráf","blue","5"
"zsiráf","pink","5"
"zászló","blue","5"
"zászló","pink","5"
"zsiráf","blue","10"
"zsiráf","pink","10"

database2的内容:

SELECT PrintedFigureProductColorQTY FROM b904_eladas

(Name, color, QTY)
"vitorlás","blue","5"
"vitorlás","pink","5"
"vonat","blue","5"
"vonat","pink","5"
"zsiráf","blue","5"
"zsiráf","pink","5"
"zászló","blue","5"
"zászló","pink","5"
"zsiráf","blue","5"
"zászló","blue","10"
"zsiráf","blue","6"
"zsiráf","pink","6"
"gólya","blue","10"
"gólya","pink","10"
"hajókormány","blue","10"
"hajókormány","pink","10"
"macska","blue","10"
"macska","pink","10"
"malac","blue","10"
"malac","pink","10"
"villamos","blue","3"
"villamos","pink","3"
"virág","blue","3"
"virág","pink","3"
"virág2","blue","4"
"gólya","blue","10"
"hajókormány","blue","10"

当PrintedFigure和展位数据库中的产品颜色相同时,我需要名为QTY的列的区别。

你的结果是错的:

Image,Color,QtyV,QtyE,Qty,QtyOtherWay 
"gólya","blue","0","0","-20","-20"
"gólya","pink","0","0","-10","-10"
"hajókormány","blue","0","0","-20","-20"
"hajókormány","pink","0","0","-10","-10"
"macska","blue","0","0","-10","-10"
"macska","pink","0","0","-10","-10"
"malac","blue","0","0","-10","-10"
"malac","pink","0","0","-10","-10"
"villamos","blue","5","5","2","2"
"villamos","pink","5","5","2","2"
"virág","blue","5","5","2","2"
"virág","pink","5","5","2","2"
"virág2","blue","5","5","1","1"
"virág2","pink","5","5","5","5"
"vitorlás","blue","5","5","0","0"
"vitorlás","pink","5","5","0","0"
"vonat","blue","5","5","0","0"
"vonat","pink","5","5","0","0"
"zászló","blue","10","10","-5","-5"
"zászló","pink","5","5","0","0"
"zsiráf","blue","45","45","13","13"
"zsiráf","pink","30","30","8","8"

在尝试使用join和union之前,我遇到了完全相同的问题。

使用两个查询的代码和创建数组的while循环结果很好:

#   Name and color      Difference
1   villamos - blue     2
2   villamos - pink     2
3   virág - blue        2
4   virág - pink        2
5   virág2 - blue       1
6   virág2 - pink       5
7   vitorlás - blue     0
8   vitorlás - pink     0
9   vonat - blue        0
10  vonat - pink        0
11  zászló - blue       -10
12  zászló - pink       0
13  zsiráf - blue       -1
14  zsiráf - pink       4

我问的是一个查询的解决方案,因为我想取消使用这个循环:

            while(false !== $EladasContent = mysql_fetch_assoc($EladasResult)) {                        
                $new_array[] = $EladasContent;
                $Eladasok[$new_array[$ElLoop][EladasKep] . '-' . $new_array[$ElLoop][EladasSzin]] = $EladasContent[EladasQTY];
                $ElLoop++;
            }

答案 1 :(得分:0)

我想我终于明白了...抱歉。下面是两个解决方案(我相信第一个解决方案性能更好,但不是正面)在任何一种情况下,zsiraf + pink都是15&amp; 11与差异为4

SELECT MasterList.Name, MasterList.Color, E.EQty, V.VQty
, COALESCE(V.VQty, 0) - COALESCE(E.EQty,0) AS NetQty
FROM
(SELECT DISTINCT Name, Color
FROM b904_Eladas
UNION
SELECT DISTINCT Name, Color
FROM b904_Vasarlas) AS MasterList
LEFT JOIN 
(SELECT Name,Color, SUM(QTY) AS EQty
  FROM b904_Eladas 
  GROUP BY Name,Color
 )AS E
ON MasterList.Name = E.Name AND MasterList.Color = E.Color
LEFT JOIN 
(SELECT Name,Color, SUM(QTY) AS VQty
  FROM b904_Vasarlas 
  GROUP BY Name,Color
 )AS V
ON MasterList.Name = V.Name AND MasterList.Color = V.Color
ORDER BY 1,2

SELECT 
E.Name AS EName
, V.Name AS VName
, COALESCE(E.Name ,v.Name) AS Name
, E.Color AS EColor
, V.Color AS VColor
, COALESCE(E.Color , V.Color) AS Color
, COALESCE(E.Total_QTY,0) AS EQTY
, COALESCE(V.Total_QTY, 0) AS VQTY
, COALESCE(V.Total_QTY,0) - COALESCE(E.Total_QTY, 0) AS NETQTY
FROM
  (SELECT b904_Eladas.Name as `Name`, b904_Eladas.Color AS Color, SUM(b904_Eladas.QTY) AS Total_QTY
  FROM b904_Eladas
  GROUP BY NAME, Color) AS E
LEFT JOIN 
  (SELECT b904_Vasarlas.Name as `Name`, b904_Vasarlas.Color AS Color, SUM(b904_Vasarlas.QTY) AS Total_QTY
  FROM b904_Vasarlas
  GROUP BY NAME, Color) AS V
ON V.Name = E.Name AND V.Color = E.Color
UNION
SELECT 
E.Name AS EName
, V.Name AS VName
, COALESCE(E.Name ,v.Name) AS Name
, E.Color AS EColor
, V.Color AS VColor
, COALESCE(E.Color , V.Color) AS Color
, COALESCE(E.Total_QTY,0) AS EQTY
, COALESCE(V.Total_QTY, 0) AS VQTY
, COALESCE(V.Total_QTY,0) - COALESCE(E.Total_QTY, 0) AS NETQTY
FROM
  (SELECT b904_Eladas.Name as `Name`, b904_Eladas.Color AS Color, SUM(b904_Eladas.QTY) AS Total_QTY
  FROM b904_Eladas
  GROUP BY NAME, Color) AS E
RIGHT JOIN 
  (SELECT b904_Vasarlas.Name as `Name`, b904_Vasarlas.Color AS Color, SUM(b904_Vasarlas.QTY) AS Total_QTY
  FROM b904_Vasarlas
  GROUP BY NAME, Color) AS V
ON V.Name = E.Name AND V.Color = E.Color
ORDER BY 3,6

答案 2 :(得分:0)

您可以使用子查询从第二个表中获取总和。

SELECT v.PrintedFigure as Image, 
    v.ProductColor as Color, 
    SUM(v.QTY) - (
        SELECT SUM(e.QTY)
        FROM `b904_Eladas` e 
        WHERE e.PrintedFigure = v.PrintedFigure
          AND e.ProductColor  = v.ProductColor
    ) as QTY_diff 
FROM `b904_Vasarlas` v
GROUP BY v.PrintedFigure, v.ProductColor
ORDER by v.PrintedFigure, v.ProductColor