Skip to content
Advertisement

mysql order by field row display even if empty

I’ve two tables( bom and bom_parts). Need to inner join and display stock_deduct,GRN_id,part_id fields.

Table 1 (bom):

enter image description here

Table 2 (bom_parts):

enter image description here

MySql query:

Query Result:

enter image description here

Expected Result:

Even if the part_id field is empty in this case 8, 6, I just want to display the part_id with the stock_deduct field as 1 in the result as mentioned below.

Stock_deduct GRN_id part_id
1 6 34
2 9 34
3 GRN1 34
1 8
1 GRN3 36
2 GRN2 36
1 6

As you can see the row 4 and 7 in the expected result table as the specified ORDER BY FIELD(part_id, 34,8,36,6) order.

Thanks in advance.

Advertisement

Answer

I think you want a left join, and filtering:

The filter on bom.part_id in the where clause brings the relevent row of bom. The we bring the parts table with a left join, so unmatched boms are not filtered out. Finally, coalesce() turns missing stock_deduct to 1.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement