I want to select all columns from table A and just one column from table B, the information is related so I tried to join tables, but didn’t work.
The following setup can be used as example, note that genreId is a foreign key to tbGenres(id):
tbBooks
id | title | genreId |
---|---|---|
1 | ‘title1’ | 4 |
2 | ‘title2’ | 4 |
3 | ‘title3’ | 2 |
tbGenres
id | name |
---|---|
1 | ‘Action’ |
4 | ‘Comedy’ |
I want a query that gives me the following, based on the genre_id. So for example, when we pass 4 as param of the query:
id | title | genreId | name |
---|---|---|---|
1 | ‘title1’ | 4 | ‘Comedy’ |
2 | ‘title2’ | 4 | ‘Comedy’ |
I tried to use INNER JOIN and other JOINS with a WHERE clause, like this:
SELECT a.*, b.name from tbBooks AS a INNER JOIN tbGenres AS b WHERE a.genreId = 4
But it returned multiple repeated rows with incorrects tbGenres names.
Can someone help with this one? I also thought of using UNION but I can’t since I don’t have the same number of columns in both tables.
2
Answers
You can use the
INNER JOIN
clause with the correctON
condition. . Here is the corrected code:Here is the fiddle link
Some comments on your question
‘INNER JOIN and , (comma) are semantically equivalent in the absence of a join condition: both produce a Cartesian product’ -https://dev.mysql.com/doc/refman/8.0/en/join.html
your query segment
results in
with the condition
results in
Changing the condition to
but the better code is supplied in the answer from @User12345
a UNION is a a vertical join and not the result you want AND you are wrong to think you cannot union tables with different number of columns, you can supply dummy values if need be. You also should name columns rather than use *