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