0

2 つのクエリがあります。

(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 列でグループ化された 1 つの mysql クエリのみを使用して、2 つのテーブルの QTY 列の差を取得するにはどうすればよいですか?


最後に、この長いコードを使用しましたが、より単純なクエリ文字列を 1 つだけ使用して問題を解決できると思います。

$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

database1 の内容:

選択PrintedFigure、、、からProductColor_QTYb904_vasarlas

(名前、色、数量)
"ビジャモス","ブルー","5"
"ヴィラモス"、"ピンク"、"5"
"virág","blue","5"
"virág"、"ピンク"、"5"
"virág2","青","5"
"virág2"、"ピンク"、"5"
"vitorlás","blue","5"
"vitorlás","pink","5"
"vonat","blue","5"
「vonat」、「ピンク」、「5」
"zsiráf","blue","5"
"zsiráf","pink","5"
"zászló","blue","5"
「zászló」、「ピンク」、「5」
"zsiráf","blue","10"
"zsiráf","pink","10"

database2 の内容:

選択PrintedFigure、、、からProductColor_QTYb904_eladas

(名前、色、数量)
"vitorlás","blue","5"
"vitorlás","pink","5"
"vonat","blue","5"
「vonat」、「ピンク」、「5」
"zsiráf","blue","5"
"zsiráf","pink","5"
"zászló","blue","5"
「zászló」、「ピンク」、「5」
"zsiráf","blue","5"
"zászló","blue","10"
"zsiráf","blue","6"
"zsiráf","pink","6"
「ゴリヤ」、「ブルー」、「10」
「ゴリヤ」、「ピンク」、「10」
"hajókormány","blue","10"
"hajókormány","ピンク","10"
"macska","blue","10"
「macska」、「ピンク」、「10」
「マラック」、「ブルー」、「10」
「マラク」、「ピンク」、「10」
"ヴィラモス","ブルー","3"
"ヴィラモス"、"ピンク"、"3"
"virág","blue","3"
"virág"、"ピンク"、"3"
"virág2"、"青"、"4"
「ゴリヤ」、「ブルー」、「10」
"hajókormány","blue","10"

ブース データベースで PrintedFigure と製品の色が同じ場合、QTY という列の差が必要です。

あなたの結果は間違っています:

イメージ、カラー、数量V、数量E、数量、数量その他の方法
"golya","blue","0","0","-20","-20"
"ゴリア"、"ピンク"、"0"、"0"、"-10"、"-10"
"hajókormány","blue","0","0","-20","-20"
"hajókormány","ピンク","0","0","-10","-10"
"macska","blue","0","0","-10","-10"
"macska","ピンク","0","0","-10","-10"
"マラク","青","0","0","-10","-10"
「マラク」、「ピンク」、「0」、「0」、「-10」、「-10」
"ヴィラモス","青","5","5","2","2"
"ヴィラモス","ピンク","5","5","2","2"
"virág","blue","5","5","2","2"
"virág"、"ピンク"、"5"、"5"、"2"、"2"
"virág2"、"青"、"5"、"5"、"1"、"1"
"virág2"、"ピンク"、"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 を使用しようとしたときとまったく同じ問題がありました。

結果は、2 つのクエリと配列作成の while ループを使用するコードで良好です。

# 名前と色の違い
1 ヴィラモス - 青 2
2 ヴィラモ - ピンク 2
3 virág - 青 2
4 virág - ピンク 2
5 virág2 - 青 1
6 virág2 - ピンク 5
7 ビトルラス - 青 0
8 ビトルラス - ピンク 0
9 vonat - 青 0
10 vonat - ピンク 0
11 ザスロ - 青 -10
12 ザスロ - ピンク 0
13 zsiráf - 青 -1
14 ジラーフ - ピンク 4

このループを使用してキャンセルしたいので、1 つのクエリの解決策を尋ねています。

            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

やっと理解できた気がします…申し訳ありません。以下に 2 つの解決策を示します (パフォーマンスに関しては最初の方が優れていると思いますが、肯定的ではありません) どちらの場合も、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 に答える