Saravana Kumar Saravana Kumar - 1 month ago 4
MySQL Question

How to combine these mysql queries

Query 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";


Query 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";


Please Help me...

Answer

Try With this code

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

That is like

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