How to do a Select statement inside the main select statement in MySQL

Filed Under (General) by Wenbert on 18-03-2008

Tagged Under :

A little bit slow but it gets the job done. I used this to generate a report to get the total open_amount for customers.

  1.  
  2.     SELECT
  3.     s.parent_no,
  4.     s.parent_name,
  5.     t.parent_no,
  6.     t.col_manager,
  7.     t.invoice_date,
  8.     t.due_date,
  9.     t.over_7_date,
  10.     t.over_7_by_eom,
  11.     t.open_amount,
  12.  
  13.     (SELECT SUM(t2.open_amount) FROM transactions t2
  14.         WHERE
  15.         (t2.parent_no=t.parent_no)
  16.         AND t2.trans_status = ‘open’) AS total
  17.        
  18.     FROM
  19.     transactions t
  20.     LEFT JOIN summary s ON s.parent_no = t.parent_no
  21.     WHERE
  22.     ( t.col_manager=‘aaa’ OR
  23.     t.col_manager = ‘bbb’  OR
  24.     t.col_manager = ‘ccc’  OR
  25.     t.col_manager = ‘ddd’  OR
  26.     t.col_manager = ‘eee’  OR
  27.     t.col_manager = ‘fff’  )
  28.     AND t.trans_status = ‘open’ ORDER BY s.parent_name ASC
  29.  

Leave a Reply

Subscribe to Rss Feed : Rss