gpt4 book ai didi

mysql - SQL 两个表的左连接

转载 作者:行者123 更新时间:2023-11-29 11:43:32 25 4
gpt4 key购买 nike

我正在连接数据库中的两个表。但我在显示第二个表中的数据时遇到问题。

我的第一个表包含具有多个itemcode的数据,但我想在页面中显示一次,因此在查询中使用了GROUP BY。在我的第二个表上也有多个 itemcode 但这次我想在页面上显示数据。但它没有显示,是因为 GROUP BY 的原因吗?您能帮我确定查询中有什么问题吗?谢谢。

这是我想要的示例表。

enter image description here

这是我的查询(**我简化了它):

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>&nbsp;</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>&nbsp;</td>";
echo "<td>&nbsp;</td>";
echo "<td>&nbsp;</td>";
echo "<td>&nbsp;</td>";
echo "<td>&nbsp;</td>";
echo "<td>&nbsp;</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 值老化

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;

最佳答案

如果您执行GROUP BY my_columns,则每个列值组合将只有一行。所有其他选定的列(不在 GROUP BY 中的列)都是从每个组的随机行中选择的(也许是第一个,我不记得了)。这里的 LEFT OUTER JOIN 允许行根本没有 mva 列。

GROUP BY 用于处理聚合,例如 COUNTSUMMAX。只需删除 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

关于mysql - SQL 两个表的左连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35357949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com