Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

mysql command to get data of 2 tables

I would like to get data from 2 mysql tables.

Table tbl_1:

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Table tbl_2:

enter image description here

I tried this:

SELECT `tbl_2`.* 
FROM `tbl_1` 
    LEFT JOIN `tbl_2` ON `tbl_1`.invoiceID = `tbl_2`.invoiceID 
WHERE `tbl_1`.customerID = "463";

My result:

enter image description here

The results looks good but not perfect.
I get many null rows which are not be in my result.
only the first both rows should be in the result.

Where is my fault?

>Solution :

The LEFT JOIN keyword returns all records from the left table tbl_1, and the matching records from the right table tbl_2. So change the order of the tables

SELECT `tbl_2`.* 
FROM `tbl_2` 
    LEFT JOIN `tbl_1` ON `tbl_1`.invoiceID = `tbl_2`.invoiceID 
WHERE `tbl_1`.customerID = '463';
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading