Search code examples
mysqlsqlyog

sql - UNION ALL and inner join in the other side


I want queries that want to achieve where all the same value with this my inner join should be their only value as the first value column, in the picture here:

enter image description here

and I just want to be output is the red line in the picture.

here is my queries:

  SELECT it.Date AS 'Date', it.invoice AS 'Invoice No.', 
         it.company AS 'Company', it.total_amount AS 'Total Amount'
  FROM invoicesummary_tbl it
  WHERE it.total !=0 
  UNION ALL
  SELECT itt.Date AS 'Date',rt.revises_no AS 'Invoice_No.', itt.company AS 
  'Company', 
       itt.total_amount AS 'Total Amount'   
  FROM revises_tbl rt
  INNER JOIN invoicesummary_tbl itt
  ON itt.invoice=rt.invoice

Solution

  • Use DISTINCT keyword:

    SELECT DISTINCT *
      FROM (SELECT it.Date AS 'Date',
                   it.invoice AS 'Invoice No.',
                   it.company AS 'Company',
                   it.total_amount AS 'TotalAmount'
              FROM invoicesummary_tbl it
             WHERE it.total != 0
            UNION ALL
            SELECT itt.Date AS 'Date',
                   rt.revises_no AS 'Invoice_No.',
                   itt.company AS 'Company',
                   itt.total_amount AS 'Total Amount'
              FROM revises_tbl rt
                   INNER JOIN invoicesummary_tbl itt ON itt.invoice = rt.invoice
    ) AS a