如何组合这些mysql查询

时间:2016-02-25 07:18:17

标签: php mysql

查询1

$query = "SELECT stock_issue.*,stock_item.item_name 
  FROM stock_issue,stock_item
  WHERE stock_issue.idate>=$from 
    AND stock_issue.idate<=$to 
    AND stock_issue.item_id=stock_item.ID 
ORDER BY stock_issue.idate DESC";

查询2

$query = "SELECT stock_purchase.*,stock_item.item_name
  FROM stock_purchase,stock_item 
  WHERE stock_purchase.pdate>=$from 
    AND stock_purchase.pdate<=$to
    AND stock_purchase.itemid=stock_item.ID 
  ORDER BY stock_purchase.pdate DESC";

请帮助我......

3 个答案:

答案 0 :(得分:2)

尝试使用此代码

SELECT stock_issue.*,stock_item.item_name 
  FROM stock_issue,stock_item
  WHERE stock_issue.idate>='$from' 
    AND stock_issue.idate<='$to' 
    AND stock_issue.item_id=stock_item.ID 
ORDER BY stock_issue.idate DESC

UNION

SELECT stock_purchase.*,stock_item.item_name
  FROM stock_purchase,stock_item 
  WHERE stock_purchase.pdate>='$from' 
    AND stock_purchase.pdate<='$to'
    AND stock_purchase.itemid=stock_item.ID 
  ORDER BY stock_purchase.pdate DESC

就像

$sql=$query1.'UNION'.$query2;

答案 1 :(得分:0)

您可以使用联合来组合它。

(SELECT stock_issue.*,stock_item.item_name FROM stock_issue,stock_item where stock_issue.idate>=$from and stock_issue.idate<=$to and stock_issue.item_id=stock_item.ID) UNION (SELECT stock_purchase.*,stock_item.item_name FROM stock_purchase,stock_item where stock_purchase.pdate>=$from and stock_purchase.pdate<=$to and stock_purchase.itemid=stock_item.ID)

答案 2 :(得分:0)

使用UNION

尝试此操作
  $sql="SELECT stock_issue.*,stock_item.item_name FROM stock_issue,stock_item where stock_issue.idate>=$from and stock_issue.idate<=$to and stock_issue.item_id=stock_item.ID ORDER BY stock_issue.idate DESC
         UNION
        SELECT stock_purchase.*,stock_item.item_name FROM stock_purchase,stock_item where stock_purchase.pdate>=$from and stock_purchase.pdate<=$to and stock_purchase.itemid=stock_item.ID ORDER BY stock_purchase.pdate DESC";