MySQL LEFT JOIN is returning just one row with a where condition on the right table

BlackBishop

I have two tables, one with events and a second one with events marked as saved by a user.

I need to get all the events, and for a certain user to know whether an event is saved or not.

For doing that I am using this SQL

SELECT event.id, saved.user_id
FROM event
LEFT JOIN saved on event.id = saved.event_id

but then I am getting repeated events, as one event may be marked as saved by many users at the saved table.

Therefore I've added a where condition as

SELECT event.id, saved.user_id
FROM event
LEFT JOIN saved on event.id = saved.event_id
where saved.user_id = "71"

but surprisingly, the result obtained is reduced to just one row, when I was expecting thousands of them, most of them with saved.user_id to null and only a few or just once with saved.user_id = 71

Ideally I would like to have true/false at the saved.user_id column for a certain user, eg: 71

What is wrong here?

Andomar

Change:

LEFT JOIN saved on event.id = saved.event_id
where saved.user_id = "71"

To:

LEFT JOIN saved on event.id = saved.event_id
                   and saved.user_id = "71"

Or:

LEFT JOIN saved on event.id = saved.event_id
WHERE saved.user_id = "71" OR saved.user_id IS NULL

The where clause filters data from all tables, the on clause just filters the rows from the right-hand table.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mysql left join with condition in right table

From Dev

Mysql left join with condition in right table

From Dev

Mysql LEFT JOIN same table with WHERE condition

From Dev

Mysql LEFT JOIN same table with WHERE condition

From Dev

left join with where in right table

From Dev

LEFT OUTER JOIN selects just one row

From Dev

Perform right outer join with a condition for left table

From Dev

Mysql Left join with One row

From Dev

query for join two column and get one row per left with column that have array of right table refrenced to left table row

From Dev

mysql LEFT JOIN with condition from other table

From Dev

Where clause for only one table in a left join

From Dev

MySQL JOIN returning unrelated rows when combined with LEFT JOIN, WHERE and OR

From Dev

How do I do a LEFT JOIN in MySQL, where I have missing key values in the RIGHT table?

From Dev

What is the proper syntax for multiple left join and where condition on the left table?

From Dev

MySQL left join limit to one row

From Dev

MySQL left join limit to one row

From Dev

mysql left join and group by not returning all results in left table

From Dev

mysql left join - returning rows using NULL and a condition

From Dev

left join tables, use only one row from left table

From Dev

Laravel Left join add where clause to right table

From Dev

Laravel Left join add where clause to right table

From Dev

MySQL LEFT JOIN with OR condition

From Dev

mysql return one row of right table

From Dev

How to return only one row from the right-most table using mysql join

From Dev

MySql LEFT JOIN returns only one NULL row from the other table

From Dev

MySQL join tables where all left records are in right

From Dev

LEFT JOIN WHERE not returning results

From Dev

right join table status with number of member count with where / having condition

From Dev

GROUP BY on table returning incorrect counts in MySQL with LEFT JOIN

Related Related

  1. 1

    Mysql left join with condition in right table

  2. 2

    Mysql left join with condition in right table

  3. 3

    Mysql LEFT JOIN same table with WHERE condition

  4. 4

    Mysql LEFT JOIN same table with WHERE condition

  5. 5

    left join with where in right table

  6. 6

    LEFT OUTER JOIN selects just one row

  7. 7

    Perform right outer join with a condition for left table

  8. 8

    Mysql Left join with One row

  9. 9

    query for join two column and get one row per left with column that have array of right table refrenced to left table row

  10. 10

    mysql LEFT JOIN with condition from other table

  11. 11

    Where clause for only one table in a left join

  12. 12

    MySQL JOIN returning unrelated rows when combined with LEFT JOIN, WHERE and OR

  13. 13

    How do I do a LEFT JOIN in MySQL, where I have missing key values in the RIGHT table?

  14. 14

    What is the proper syntax for multiple left join and where condition on the left table?

  15. 15

    MySQL left join limit to one row

  16. 16

    MySQL left join limit to one row

  17. 17

    mysql left join and group by not returning all results in left table

  18. 18

    mysql left join - returning rows using NULL and a condition

  19. 19

    left join tables, use only one row from left table

  20. 20

    Laravel Left join add where clause to right table

  21. 21

    Laravel Left join add where clause to right table

  22. 22

    MySQL LEFT JOIN with OR condition

  23. 23

    mysql return one row of right table

  24. 24

    How to return only one row from the right-most table using mysql join

  25. 25

    MySql LEFT JOIN returns only one NULL row from the other table

  26. 26

    MySQL join tables where all left records are in right

  27. 27

    LEFT JOIN WHERE not returning results

  28. 28

    right join table status with number of member count with where / having condition

  29. 29

    GROUP BY on table returning incorrect counts in MySQL with LEFT JOIN

HotTag

Archive