alena1347 Posted February 13, 2013 Share Posted February 13, 2013 I have a query by $ques=mysql_query("SELECT field.field FROM field LEFT OUTER JOIN skill_freelancer ON skill_freelancer.sname=field.field WHERE skill_freelancer.id=$sr"); here $sr is a id 1 my table are field skill_freelancer ------------------- -------------------------- id | field id | sname 1 | html 1 | html 2 | php 1 | php 3 | c The output is html,php but i need only "c" could anyone help in the query Link to comment https://forums.phpfreaks.com/topic/274432-left-outer-join-query/ Share on other sites More sharing options...
Barand Posted February 13, 2013 Share Posted February 13, 2013 skill_freelancer is "LEFT JOINED" so you can't put conditions in the WHERE clause, they need to be part of the JOIN condition SELECT field.field FROM field LEFT OUTER JOIN skill_freelancer ON skill_freelancer.sname=field.field AND skill_freelancer.id=$s Link to comment https://forums.phpfreaks.com/topic/274432-left-outer-join-query/#findComment-1412183 Share on other sites More sharing options...
alena1347 Posted February 13, 2013 Author Share Posted February 13, 2013 I have a query by $ques=mysql_query("SELECT field.field FROM field LEFT OUTER JOIN skill_freelancer ON skill_freelancer.sname=field.field WHERE skill_freelancer.id=$sr"); here $sr is a id 1 my table are field ------------------- id | field 1 | html 2 | php 3 | c skill_freelancer -------------------------- id | sname 1 | html 1 | php The output is html,php but i need only "c" could anyone help in the query OK barand but could you show me a way to get only the skill c Link to comment https://forums.phpfreaks.com/topic/274432-left-outer-join-query/#findComment-1412186 Share on other sites More sharing options...
Barand Posted February 13, 2013 Share Posted February 13, 2013 SELECT field.field FROM field LEFT OUTER JOIN skill_freelancer ON skill_freelancer.sname=field.field AND skill_freelancer.id=$s WHERE skill_freelancer.sname IS NULL Link to comment https://forums.phpfreaks.com/topic/274432-left-outer-join-query/#findComment-1412192 Share on other sites More sharing options...
alena1347 Posted February 13, 2013 Author Share Posted February 13, 2013 SELECT field.field FROM field LEFT OUTER JOIN skill_freelancer ON skill_freelancer.sname=field.field AND skill_freelancer.id=$sr WHERE skill_freelancer.sname IS NULL Thank you for this query it solved the problem just replace the $s with $sr , could you please explain this query in words Link to comment https://forums.phpfreaks.com/topic/274432-left-outer-join-query/#findComment-1412205 Share on other sites More sharing options...
Barand Posted February 13, 2013 Share Posted February 13, 2013 First a bit of basic join theory. SELECT * FROM A INNER JOIN B ON A.key = B.key The INNER JOIN means get only data from records matching on key in both files SELECT * FROM A LEFT JOIN B ON A.key = B.key LEFT JOIN above means get all records from A with the matching data from B if it exists. Where there is no matching record in B, the fields from B contain NULL. So with your query, limiting the selection to freelancer 1 SELECT field.field, skill_freelancer.id, skill_freelancer.sname FROM field LEFT OUTER JOIN skill_freelancer ON skill_freelancer.sname=field.field AND skill_freelancer.id=1 we get +-------+------+-------+ | field | id | sname | +-------+------+-------+ | html | 1 | html | | php | 1 | php | | c | NULL | NULL | +-------+------+-------+ You want listed the fields that are not in skill_freelancer for id 1 - ie where sname is NULL, so we add "WHERE sname IS NULL" to show only those results. Link to comment https://forums.phpfreaks.com/topic/274432-left-outer-join-query/#findComment-1412223 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.