Jump to content

left outer join query


alena1347

Recommended Posts

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

Edited by alena1347
Link to comment
Share on other sites

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

Edited by Barand
Link to comment
Share on other sites

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
Share on other sites

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

Edited by alena1347
Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.