question

raouafi avatar image
raouafi asked

SQL Join

Hello, I have two tables A(id,user_id,info,...) and B(B_id,A_id), I'm unable to write the SQL request that gives me all fields of a given user from the table A with B_id if it exist. Could any one help me please ?
join
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Wilfred van Dijk avatar image
Wilfred van Dijk answered
If you use this code : select A.*, B.* from table_a as A join table_b as B on A.id = B.A_id only the matching ones are showed. If you change "join" to "left join", everything from table_a is shown and fields from table_b are NULL if there's no match. see [this article][1] [1]: https://www.mssqltips.com/sqlservertip/1667/sql-server-join-example/
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Tom Staab avatar image Tom Staab ♦ commented ·
If I understand the requirements correctly, I believe the columns should be A.*, B.B_id.
0 Likes 0 ·
Wilfred van Dijk avatar image Wilfred van Dijk commented ·
code clarified
0 Likes 0 ·
raouafi avatar image raouafi commented ·
It works when modified this way : SELECT A.* , B.* FROM table_a AS A LEFT JOIN table_b AS B ON A.id = B.A_id WHERE A.userId = MyUser; Thanks a lot.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.