0

我有两个疑问:

(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"

如何仅通过 Image AND color 列分组的一个 mysql 查询来获取两个表的 QTY 列的差异?


最后我使用了这个更长的代码,但我想我可以只用一个更简单的查询字符串来解决这个问题。

$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>';
4

3 回答 3

0

The content of database1:

SELECT PrintedFigure, ProductColor, QTY 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"

The content of database2:

SELECT PrintedFigure, ProductColor, QTY 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"

I need the difference of column called QTY when PrintedFigure AND the product color is same in booth databases.

Your result is wrong:

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"

I have exactly same problem before when I tried using join and union.

The result is good with code using two queries and while loop for array creation:

#   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

I am asking the solution of one query because I want to cancel using this loop:

            while(false !== $EladasContent = mysql_fetch_assoc($EladasResult)) {                        
                $new_array[] = $EladasContent;
                $Eladasok[$new_array[$ElLoop][EladasKep] . '-' . $new_array[$ElLoop][EladasSzin]] = $EladasContent[EladasQTY];
                $ElLoop++;
            }
于 2013-09-29T09:36:42.173 回答
0

我想我终于明白了……对不起。下面是两个解决方案(我相信第一个对性能更好,但不是正面的)在任何一种情况下,zsiraf+pink 都是 15 和 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
于 2013-10-01T16:18:46.467 回答
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
于 2017-05-07T16:46:09.307 回答