我有两个疑问:
(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>';