I have a query which joins two table. Invoice table and comments table. But there are instances that an invoice has no equivalent row in Comments table.
My query returns those who has both, Example is My Invoice ID 1 has two rows in Comments table thus returning concatenated 2 rows in Comment table and joins it to my invoice table, but what if I also want to display those who has no comments in Comments table.
Sample is below
Table 1 (INVOICE TABLE) TABLE 2 (COMMENTS TABLE)
ID NAME ID COMMENTS
1 MYINVOCIE 1 YEAH
2 MY OTHER INVOICE 1 ALRIGHT
MY QUERY WILL RETURN IT AS
TABLE WITH CONCAT AND JOINED
ID NAME COMMENTS
1 MY INVOICE YEAH|ALRIGHT
But it does not show the 2 because in my query it checks if the ID has an equal ID on the table 2. I want my result to be like
TABLE I WANT TO HAVE AS A RESULT
ID NAME COMMENT
1 MY INVOICE YEAH|ALRIGHT
2 MY OTHER INVOICE NULL
Here's my QUERY
SELECT `main_table`.*, 
(
 SELECT GROUP_CONCAT(comment SEPARATOR '|' ) 
 FROM mgmx_sales_flat_invoice_comment a WHERE a.parent_id = 
 main_table.entity_id 
 group by parent_id
) AS `comment` 
FROM `mgmx_sales_flat_invoice_grid` AS `main_table`
INNER JOIN `mgmx_sales_flat_invoice_comment` AS `a` 
ON a.parent_id = main_table.entity_id 
GROUP BY `main_table`.`entity_id`
1 Answer 1
I found it. It has something to do with joining.
I rewrite my query to this.
SELECT `main_table`.*, 
(
 SELECT GROUP_CONCAT(comment SEPARATOR '|' ) 
 FROM mgmx_sales_flat_invoice_comment a WHERE a.parent_id = 
 main_table.entity_id 
 group by parent_id
) AS `comment` 
FROM `mgmx_sales_flat_invoice_grid` AS `main_table`
LEFT JOIN `mgmx_sales_flat_invoice_comment` AS `a` 
ON a.parent_id = main_table.entity_id 
 GROUP BY `main_table`.`entity_id`