Saturday 23 July 2016

Learn about left join query

The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.

In some databases LEFT JOIN is called LEFT OUTER JOIN.

Query:-

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;

OR

SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;

Now,Join Query with objects:-

Query 1 simple
------------------

select t.*,c.* from time as t  LEFT JOIN country as c ON t.countryId=c.id 
Query 2 long 
------------------
select t.*,c.*,st.*,d.* from time as t  LEFT JOIN country as c ON t.countryId=c.id LEFT JOIN  states as st ON t.stateId=st.id LEFT JOIN date as d ON t.dateId=d.id 
Query 3 in order
--------------------
select t.*,c.*,st.*,d.* from time as t  LEFT JOIN country as c ON t.countryId=c.id LEFT JOIN  states as st ON t.stateId=st.id LEFT JOIN date as d ON t.dateId=d.id  ORDER BY name
Query 4 conditional
--------------------
select t.*,c.*,st.*,d.* from time as t  LEFT JOIN country as c ON t.countryId=c.id LEFT JOIN  states as st ON t.stateId=st.id LEFT JOIN date as d ON t.dateId=d.id  ORDER BY name where usr_id=$id



For more Interesting, Useful Article & codes visit IT New Code.

Ankit Shah PHP Expert

IT New Code suggest a solution for your problem related to coding, Specially .PHP, Wordpress, WooCommerce, Magento, HTML and CSS. Visit Our website for Information.

0 comments:

Post a Comment

 

Copyright @ 2016 IT New Code | Developing Code | Designing Code.

Designed by: Ankit Shah