0

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`
asked Nov 20, 2017 at 16:03

1 Answer 1

0

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`
answered Nov 20, 2017 at 16:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.