Matee Gojra Matee Gojra - 1 year ago 54
MySQL Question

PHP Join Query for Three Table When There is No Direct Common Column?

I have Three Tables in which first two tables have common column to match the record, and 2nd and third table also have common column but there is no direct matching column in first and third table. How shall I write join query ?

  1. Table1(order) Column Names are 'order_id', 'patient_id', 'total', 'discount'

  2. Table2 (order_details) Column Names are 'order_details_id', 'order_id',
    'test_id', 'result'

  3. Table3(tests) Column Names are 'test_id', 'test_name', 'test_normal_value'

Answer Source

I hope it helps

SELECT * FROM `order`
LEFT JOIN `order_details` ON `order`.`order_id` = `order_details`.`order_id`
LEFT JOIN `tests` ON `order_details`.`test_id` = `test`.`test_id`
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download