Mysql left join with condition in right table

Subash L

I have been trying to solve this issue for a while, hope anyone help me. I am having two table, the first table is

Table Name : OnlineTest

OnlineTestId     category    subcategory                                                                   
     1            English      Spelling                                                                    
     2            English      Grammar
     3            English      Antonyms
     4            English      Synonyms

The second table is

Table Name : UserStatus

Id     userId    status         onlineTestId
1       1        Finished           1
2       1        Not Finished       2
3       2        Not Finished       1
4       2        Finished           3
5       3        Not Finished       4

Result

OnlineTestId    userId        status
    1               1         Finished
    2               1         Not Finished
    3               null      null
    4               null      null

I have tried this query,

select c.onlinetestid, d.userid, d.status from onlinetest c left join userstatus d on d.onlinetestid = c.onlinetestid
where c.category = 'English' and d.userid = 1;

But this query is bring the first two row of the result and not the last two, in which the userId and status are null.

How to bring the above result?

Giorgos Betsos

Place the d.userid = 1 predicate in the ON clause:

select c.onlinetestid, d.userid, d.status 
from onlinetest c 
left join userstatus d on d.onlinetestid = c.onlinetestid and d.userid = 1
where c.category = 'English' 

This will return all rows from onlinetest, having columns of userstatus filled with nulls where predicate d.userid = 1 fails.

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

Perform right outer join with a condition for left table

From Dev

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

From Dev

mysql LEFT JOIN with condition from other table

From Dev

Mysql LEFT JOIN same table with WHERE condition

From Dev

Mysql LEFT JOIN same table with WHERE condition

From Dev

MySQL LEFT JOIN with OR condition

From Dev

mySQL LEFT JOIN and COUNT number of occurrences in right hand table

From Dev

Left join with on a limited right table

From Dev

left join with where in right table

From Dev

MySQL left join count with condition

From Dev

MySQL: Left Join RIGHT JOIN GROUP BY Error

From Dev

mysql left join / right join 3 tables

From Dev

Mysql left join, get all right table columns and 2 columns from left table based on left table max id

From Dev

PHP / MYSQL - display only left join items in table but concatenate right join values in a column

From Dev

What is the "left" and "right" table in an intermediate SQL Join?

From Dev

SQLite left join with two conditions in right table

From Dev

What is the "left" and "right" table in an intermediate SQL Join?

From Dev

LEFT JOIN missing values from the right table

From Dev

SQL Left Join - Multiple Rows in Right Table

From Dev

mysql left join with two table

From Dev

left/right/inner join on mysql statement

From Dev

LEFT OUTER JOIN With Condition In Third Table

From Dev

select left join table use and condition

From Dev

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

From Dev

MySQL case statement with left join condition

From Dev

MySQL left join with additional like condition

From Dev

How to use if condition with left join in mysql

From Dev

Mysql select count on left join with condition not working

Related Related

  1. 1

    Mysql left join with condition in right table

  2. 2

    Perform right outer join with a condition for left table

  3. 3

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

  4. 4

    mysql LEFT JOIN with condition from other table

  5. 5

    Mysql LEFT JOIN same table with WHERE condition

  6. 6

    Mysql LEFT JOIN same table with WHERE condition

  7. 7

    MySQL LEFT JOIN with OR condition

  8. 8

    mySQL LEFT JOIN and COUNT number of occurrences in right hand table

  9. 9

    Left join with on a limited right table

  10. 10

    left join with where in right table

  11. 11

    MySQL left join count with condition

  12. 12

    MySQL: Left Join RIGHT JOIN GROUP BY Error

  13. 13

    mysql left join / right join 3 tables

  14. 14

    Mysql left join, get all right table columns and 2 columns from left table based on left table max id

  15. 15

    PHP / MYSQL - display only left join items in table but concatenate right join values in a column

  16. 16

    What is the "left" and "right" table in an intermediate SQL Join?

  17. 17

    SQLite left join with two conditions in right table

  18. 18

    What is the "left" and "right" table in an intermediate SQL Join?

  19. 19

    LEFT JOIN missing values from the right table

  20. 20

    SQL Left Join - Multiple Rows in Right Table

  21. 21

    mysql left join with two table

  22. 22

    left/right/inner join on mysql statement

  23. 23

    LEFT OUTER JOIN With Condition In Third Table

  24. 24

    select left join table use and condition

  25. 25

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

  26. 26

    MySQL case statement with left join condition

  27. 27

    MySQL left join with additional like condition

  28. 28

    How to use if condition with left join in mysql

  29. 29

    Mysql select count on left join with condition not working

HotTag

Archive