我正在加入我的数据库中的两个表。但是我在显示第二个表中的数据时遇到了问题。
我的第一个表格中包含多个项目代码,但我想在页面中显示一次,因此在查询中使用了GROUP BY。在我的第二个表上还有多个项目代码,但这次我想在页面上显示数据。但它没有显示,是因为GROUP BY。你能帮我查一下查询中的问题吗?感谢。
这是我想看的样本表。
这是我的查询(**我简化了它):
SELECT mcc.ALL, mva.ALL
FROM `mtrl_cons_cost` mcc
LEFT JOIN material_value_aging mva ON mcc.itemcode = mva.item_code
GROUP BY mcc.site_cons , mcc.itemcode, mcc.deptcode , mcc.item_desc, mcc.subinv_code , mcc.item_uom
更新 这是整个代码。请参阅。
$site = "112";
$date1 = date("Y-m");
$date2 = date("Y-m", strtotime("-1 month") ) ;
$date3 = date("Y-m", strtotime("-2 month") ) ;
$date1s = date("Ym");
$date2s = date("Ym", strtotime("-1 month") ) ;
$date3s = date("Ym", strtotime("-2 month") ) ;
echo "<form>";
echo "<h2>CONSUMPTION REPORT AND MATERIAL VALUE AGING AS OF $date1</h2>";
echo "<table border=5>";
echo "<tr class=header bgcolor=#adff2f rowspan=2>";
echo "<th>SITE</th>";
echo "<th>DEPT</th>";
echo "<th>ITEM CODE</th>";
echo "<th>DESCRIPTION</th>";
echo "<th>ITEM TYPE</th>";
echo "<th>UOM</th>";
echo "<th>{$date1} QUANTITY</th>";
echo "<th>{$date1} COST</th>";
echo "<th>{$date2} QUANTITY</th>";
echo "<th>{$date2} COST</th>";
echo "<th>{$date3} QUANTITY</th>";
echo "<th>{$date3} COST</th>";
echo "<th bgcolor=yellow></th>";
/*** Material Value Aging ***/
//echo "<th>SITE</th>";
echo "<th>CUSTOMER PN</th>";
//echo "<th>ITEM CODE</th>";
echo "<th>TYPE</th>";
echo "<th>UOM</th>";
echo "<th>DESCRIPTION</th>";
//echo "<th>DEPT CODE</th>";
echo "<th>SUB</th>";
echo "<th>RECEIVED DATE</th>";
echo "<th>AGE</th>";
echo "<th>LOT SERIAL</th>";
echo "<th>QUANTITY</th>";
echo "<th>VALUE IN USD</th>";
echo "<th>A30 0-30DAYS</th>";
echo "<th>A60 31-60DAYS</th>";
echo "<th>A90 61-90DAYS</th>";
echo "<th>A120 91-120DAYS</th>";
echo "<th>A180 121-180DAYS</th>";
echo "<th>ABOVE 180 DAYS</th>";
echo "</tr>";
$query="
SELECT mcc.site_cons , mcc.deptcode , mcc.itemcode , mcc.item_desc , mcc.subinv_code , mcc.item_uom , SUM(
CASE WHEN DATE_FORMAT( mcc.trans_date , '%Y-%m' ) = '$date1'
THEN mcc.qty
END ) AS 'qty_$date1', SUM(
CASE WHEN DATE_FORMAT( mcc.trans_date , '%Y-%m' ) = '$date1s'
THEN mcc.total_cost
END ) AS 'tot_cost_$date1s', SUM(
CASE WHEN DATE_FORMAT( mcc.trans_date , '%Y-%m' ) = '$date2'
THEN mcc.qty
END ) AS 'qty_$date2', SUM(
CASE WHEN DATE_FORMAT( mcc.trans_date , '%Y-%m' ) = '$date2s'
THEN mcc.total_cost
END ) AS 'tot_cost_$date2s', SUM(
CASE WHEN DATE_FORMAT( mcc.trans_date , '%Y-%m' ) = '$date3'
THEN mcc.qty
END ) AS 'qty_$date3', SUM(
CASE WHEN DATE_FORMAT( mcc.trans_date , '%Y-%m' ) = '$date3s'
THEN mcc.total_cost
END ) AS 'tot_cost_$date3s', mva.*
FROM `mtrl_cons_cost` mcc
LEFT JOIN material_value_aging mva ON mcc.itemcode = mva.item_code
WHERE mcc.site_cons LIKE '$site'
GROUP BY mcc.site_cons , mcc.itemcode, mcc.deptcode , mcc.item_desc, mcc.subinv_code , mcc.item_uom
";
//
$conn = dbConnect();
$stmt = $conn->prepare($query);
$stmt-> execute();
while($getrow=$stmt->fetch(PDO::FETCH_ASSOC)) {
$site_cr = $getrow['site_cons'];
$deptcode_cr = $getrow['deptcode'];
$itemcode_cr = $getrow['itemcode'];
$itemdesc_cr = $getrow['item_desc'];
$subinvcode_cr = $getrow['subinv_code'];
$itemuom_cr = $getrow['item_uom'];
$qtydate1s_cr = $getrow['qty_$date1s'];
$costdate1s_cr = $getrow['tot_cost_$date1s'];
$qtydate2s_cr = $getrow['qty_$date2s'];
$costdate2s_cr = $getrow['tot_cost_$date2s'];
$qtydate3s_cr = $getrow['qty_$date3s'];
$costdate3s_cr = $getrow['tot_cost_$date3s'];
$sitec = $getrow['site'];
$cust_pn = $getrow['customer_pn'];
$itemcode = $getrow['item_code'];
$itemtype = $getrow['item_type'];
$UOM = $getrow['uom'];
$desc = $getrow['description'];
$deptcode = $getrow['dept_code'];
$subcode = $getrow['subinventory_code'];
$received_Date = $getrow['received_date'];
$age = $getrow['age'];
$lot_no = $getrow['lot_number'];
$qty = $getrow['qty'];
$ttl_val = $getrow['ttl_value'];
echo "<tr>";
echo "<td>{$site_cr}</td>";
echo "<td>{$deptcode_cr}</td>";
echo "<td>{$itemcode_cr}</td>";
echo "<td>{$itemdesc_cr}</td>";
echo "<td>{$subinvcode_cr}</td>";
echo "<td>{$itemuom_cr}</td>";
echo "<td>{$qtydate1s_cr}</td>";
echo "<td>{$costdate1s_cr}</td>";
echo "<td>{$qtydate2s_cr}</td>";
echo "<td>{$costdate2s_cr}</td>";
echo "<td>{$qtydate3s_cr}</td>";
echo "<td>{$costdate3s_cr}</td>";
echo "<td bgcolor=yellow> </td>";
//echo "<td>{$sitec}</td>";
echo "<td>{$cust_pn}</td>";
//echo "<td>{$itemcode}</td>";
echo "<td>{$itemtype}</td>";
echo "<td>{$UOM}</td>";
echo "<td>{$desc}</td>";
//echo "<td>{$deptcode}</td>";
echo "<td>{$subcode}</td>";
echo "<td>{$received_Date}</td>";
echo "<td>{$age}</td>";
echo "<td>{$lot_no}</td>";
echo "<td>{$qty}</td>";
echo "<td>{$ttl_val}</td>";
echo "<td> </td>";
echo "<td> </td>";
echo "<td> </td>";
echo "<td> </td>";
echo "<td> </td>";
echo "<td> </td>";
echo "</tr>";
}
$conn=null;
echo "</table>";
echo "</form>";
表1:mtrl_cons_cost
CREATE TABLE IF NOT EXISTS `mtrl_cons_cost` (
`site_cons` int(11) NOT NULL,
`deptcode` int(11) NOT NULL,
`trans_date` date NOT NULL,
`itemcode` varchar(15) NOT NULL,
`item_desc` varchar(500) NOT NULL,
`subinv_code` varchar(20) NOT NULL,
`item_uom` varchar(50) NOT NULL,
`unit_price` decimal(20,6) NOT NULL,
`qty` decimal(20,6) NOT NULL,
`total_cost` decimal(20,6) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
表2:material_value_aging
CREATE TABLE IF NOT EXISTS `material_value_aging` (
`site` varchar(10) NOT NULL,
`customer_pn` varchar(50) NOT NULL,
`item_code` varchar(50) NOT NULL,
`item_type` varchar(10) NOT NULL,
`uom` varchar(10) NOT NULL,
`description` varchar(100) NOT NULL,
`dept_code` int(11) NOT NULL,
`subinventory_code` varchar(10) NOT NULL,
`received_date` date NOT NULL,
`age` int(11) NOT NULL,
`lot_number` varchar(100) NOT NULL,
`qty` decimal(10,2) NOT NULL,
`ttl_value` decimal(10,2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
答案 0 :(得分:1)
如果您执行GROUP BY my_columns
,则每个列值的组合只有一行。所有其他选定的列(不在GROUP BY
中的列)都是从每个组的随机行中选择的(可能是第一个,我不记得)。在这里,LEFT OUTER JOIN
允许行根本没有mva
列。
GROUP BY
可用于聚合,例如COUNT
,SUM
或MAX
。只需删除GROUP BY
并使用ORDER BY
即可。您将排序很好 - 左侧有重复的信息 - 您只需迭代排序的结果以迭代识别您的组。
SELECT mcc.ALL, mva.ALL
FROM `mtrl_cons_cost` mcc
LEFT JOIN material_value_aging mva ON mcc.itemcode = mva.item_code
ORDER BY mcc.site_cons , mcc.itemcode, mcc.deptcode , mcc.item_desc, mcc.subinv_code , mcc.item_uom