如果这让你们大吃一惊,请耐心等待。
我的页面中有6个查询。所有查询都在同一个mysql表中运行,但是where子句不同。
目前我逐个执行这些查询。但是我需要将这些sql查询组合到单个视图或单个查询以根据material_id
显示记录。
查询如下,
$a_recieved="SELECT material_id,SUM(recieved_quantity-damage_quantity-returned_quantity) AS re_total_quantity,SUM(each_price) AS re_total_price,SUM(purchase_quantity) AS re_total_purchase FROM purchase_table WHERE material_id='".$info['material_id']."' AND added_time<NOW() AND item_status='Recieved'";
$a_purchased="SELECT material_id,SUM(purchase_quantity) AS total_p FROM purchase_table WHERE material_id='".$info['material_id']."' AND added_time<NOW() AND item_status='Purchase'"));
$a_production="SELECT material_id,SUM(recieved_quantity) AS pr_total_quantity,SUM(each_price) AS pr_total_price FROM purchase_table WHERE material_id='".$info['material_id']."' AND added_time<NOW() AND item_status='Production'";
$a_fg="SELECT material_id,SUM(recieved_quantity) AS fg_total_quantity,SUM(each_price) AS fg_total_price FROM purchase_table WHERE material_id='".$info['material_id']."' AND added_time<NOW() AND item_status='Fgoods'";
$a_returned="SELECT material_id,SUM(recieved_quantity) AS rt_total_quantity,SUM(each_price) AS rt_total_price FROM purchase_table WHERE material_id='".$info['material_id']."' AND added_time<NOW() AND item_status='Return'";
$a_transfered="SELECT material_id,SUM(recieved_quantity) AS tr_total_quantity,SUM(each_price) AS tr_total_price FROM purchase_table WHERE material_id='".$info['material_id']."' AND added_time<NOW() AND item_status='Transfer'";
对不起,这可能不是有效的方法。
有可能在单个查询中执行这些操作吗?或者我必须使用mysql Views吗?如果是这样,请告诉我这个。 感谢
答案 0 :(得分:0)
如果您的6个查询返回相同数量的列和相同类型的数据(即始终ID,数量,总价格),那么您使用UNION
的单个查询。
SELECT 'Received' as Operation, id, quantity, price FROM table1
UNION
SELECT 'Purchased', id, quantity, price FROM table2
UNION
...
答案 1 :(得分:-1)
试试这个
$query="SELECT material_id,SUM(recieved_quantity-damage_quantity-returned_quantity) AS re_total_quantity,SUM(each_price) AS re_total_price,SUM(purchase_quantity) AS re_total_purchase FROM purchase_table WHERE material_id='".$info['material_id']."' AND added_time<NOW() AND item_status='Recieved'
UNION
SELECT material_id,SUM(purchase_quantity) AS total_p FROM purchase_table WHERE material_id='".$info['material_id']."' AND added_time<NOW() AND item_status='Purchase'
UNION
SELECT material_id,SUM(recieved_quantity) AS pr_total_quantity,SUM(each_price) AS pr_total_price FROM purchase_table WHERE material_id='".$info['material_id']."' AND added_time<NOW() AND item_status='Production'
UNION
SELECT material_id,SUM(recieved_quantity) AS fg_total_quantity,SUM(each_price) AS fg_total_price FROM purchase_table WHERE material_id='".$info['material_id']."' AND added_time<NOW() AND item_status='Fgoods'
UNION
SELECT material_id,SUM(recieved_quantity) AS rt_total_quantity,SUM(each_price) AS rt_total_price FROM purchase_table WHERE material_id='".$info['material_id']."' AND added_time<NOW() AND item_status='Return'
UNION
SELECT material_id,SUM(recieved_quantity) AS tr_total_quantity,SUM(each_price) AS tr_total_price FROM purchase_table WHERE material_id='".$info['material_id']."' AND added_time<NOW() AND item_status='Transfer'";
使用SQL的UNION函数。 http://www.mysqltutorial.org/sql-union-mysql.aspx
抱歉,如果符合您的要求,则无法验证结果。 但也许你可以找到解决方法。