One table conditional join

Dmitry Malchikov

Is it possible in SQL to pick data from 2 tables by join with condition and when second table not satisfies the conditions then pick null values from it?

I've done it like this:

select * from Achievements left join (select * from PlayerAchievements where userId = 2) as t1 on t1.achievementsId = Achievements.Id

But I want to know is it possible to achieve the same result without sub-query?

Sebastian Brosch

You can use the following solution using a LEFT JOIN. The additional condition (userId = 2) is used on the mapping condition (ON) instead of WHERE. So you show all rows of the left table (Achievements) and only append the information of the right tables (PlayerAchievements) if the IDs of the achievements and the userId is matching.

SELECT *  FROM Achievements A 
    LEFT JOIN PlayerAchievements PA ON A.Id = PA.achievementsId AND PA.userId = 2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Conditional Table Join In SQL Server

From Dev

MySQL join on one table

From Dev

SQL conditional join to multiple tables in one shot

From Dev

Conditional join of table based on existence of value

From Dev

Conditional (inequality) join in data.table

From Dev

Mysql IF conditional Left Join same table twice

From Dev

How to do a conditional JOIN with table valued parameter?

From Dev

Multiple table join with possible conditional matches

From Dev

SQL join by limiting one table

From Dev

Join for more than one table

From Dev

firebird - self join on one table

From Dev

How to join one table twice

From Dev

JOIN applies to only one table?

From Dev

Oracle join to one table OR another

From Dev

MySQL, limit one table in join

From Dev

Join two table to be one record

From Dev

SQL join one table to other table twice

From Dev

Two dissimilar table join with one master table

From Dev

mysql join two table rows in one table

From Dev

join table with 2 FKs to one table

From Dev

sas merge nomatch conditional for only one table

From Dev

Hibernate one to many relationship with join table with addition columns in join table

From Dev

Join in MYSQL (Count from One table and list from one table)

From Dev

data.table with rolling join calculating average conditional on date

From Dev

Conditional binary join and update by reference using the data.table package

From Dev

Optimizing a conditional join in MySQL that depends on the character length of the source table

From Dev

Conditional join to two different tables based on 2 columns in 1 table

From Dev

How to select join multiple table without conditional in Codeigniter?

From Dev

Can I make a conditional for determinate with table I will inner join with?

Related Related

  1. 1

    Conditional Table Join In SQL Server

  2. 2

    MySQL join on one table

  3. 3

    SQL conditional join to multiple tables in one shot

  4. 4

    Conditional join of table based on existence of value

  5. 5

    Conditional (inequality) join in data.table

  6. 6

    Mysql IF conditional Left Join same table twice

  7. 7

    How to do a conditional JOIN with table valued parameter?

  8. 8

    Multiple table join with possible conditional matches

  9. 9

    SQL join by limiting one table

  10. 10

    Join for more than one table

  11. 11

    firebird - self join on one table

  12. 12

    How to join one table twice

  13. 13

    JOIN applies to only one table?

  14. 14

    Oracle join to one table OR another

  15. 15

    MySQL, limit one table in join

  16. 16

    Join two table to be one record

  17. 17

    SQL join one table to other table twice

  18. 18

    Two dissimilar table join with one master table

  19. 19

    mysql join two table rows in one table

  20. 20

    join table with 2 FKs to one table

  21. 21

    sas merge nomatch conditional for only one table

  22. 22

    Hibernate one to many relationship with join table with addition columns in join table

  23. 23

    Join in MYSQL (Count from One table and list from one table)

  24. 24

    data.table with rolling join calculating average conditional on date

  25. 25

    Conditional binary join and update by reference using the data.table package

  26. 26

    Optimizing a conditional join in MySQL that depends on the character length of the source table

  27. 27

    Conditional join to two different tables based on 2 columns in 1 table

  28. 28

    How to select join multiple table without conditional in Codeigniter?

  29. 29

    Can I make a conditional for determinate with table I will inner join with?

HotTag

Archive