How to make a left Join using a Where condition?

Asked

Viewed 7,043 times

5

I have two tables:

category
--------
id | nome | imagem

user_follow_category
--------------------
id | from | to | date

Note: the relationship of the two tables is given by category.id and by user_follow_category.to.

What I want to do is: select all categories (table category), but identify the ones being followed by user "x" in the table user_follow_category.

This is my query:

SELECT c.id, nome, image, u.id AS follow
FROM category AS c LEFT JOIN user_follow_category as u ON c.id = u.to
WHERE u.from = 74

Updating

table Category

id    nome       imagem (que ilustra a categoria na App)
1     futebol    futebol.jpg
2     tenis      tenis.jpg
3     box        box.jpg

table user_follow_category

id     from (id do usuário)     to (id da categoria)
1       74                         2
2       74                         1
3       62                         3

I want to get the following (what categories does user "74" follow?), something like:

id    nome     segue (ou alguma outra representação)
1     futebol    sim
2     tenis      sim
3     box        nao
  • Welcome to Stackoverflow Mario. First what database system do you use? Mysql, Sqlserver? Second what is your problem?

  • Thank you. I use Mysql. My problem is I cannot recover all data from the "Category" table and identify which data is in the "user_follow_category" table, but which belong to the "x" user".

3 answers

2


Your problem is that the clause WHERE deletes lines that do not have the specified user. To resolve this, move the desired condition (user "x") to the clause ON:

SELECT c.id, nome, imagem, u.id AS follow
FROM category AS c LEFT JOIN user_follow_category as u 
    ON c.id = u.to AND u.`from` = 74;

Example in Sqlfiddle. Source: that answer in Soen.

This will return the id of the relation if any exists, or NULLif none exists. For a representation closer than you ask, you can use CASE to test the column u.id is void or not:

SELECT c.id, nome, imagem, 
    case when isnull(u.id) then "não" else "sim" end AS follow
FROM category AS c LEFT JOIN user_follow_category as u 
    ON c.id = u.to AND u.`from` = 74;

Updated example.

  • Dust man, thank you very much. That’s exactly what I need. It’s worth even mgibsonbr. I have to study more Sql.

1

Try the query below:

SELECT c.id, c.nome, c.image, u.id AS follow, IF(u.id, 'Sim', 'Não') as segue FROM category c 
LEFT JOIN (user_follow_category u) ON (c.id = u.to)
WHERE u.from = 74

Note that it may be c.image as your table, because I followed the fields according to your query.

0

I believe this is what you need:

Select * from category a,user_follow_category b where a.id=b.to
  • 2

    This will return only categories that have users following, and any user, not a specific one (i.e. the same category can come multiple times). I don’t think that’s what the AP wants, but the question is not very clear so far.

  • It can add a select distinct name... which in this way will bring only the unique records.

  • 1

    So it doesn’t work. I’ll make the problem clearer. Thank you.

  • 1

    Still if a category has no user following it will not be included in the return. From what I understand, the idea is to return all the categories - so that a Outer Join it really seems to me to be the right way.

  • That’s exactly what I need @mgibsonbr. But it needs to be from a specific user.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.