I have two tables and want to make a query. I tried to get team AA and team BB's image base on table A.
I used:
SELECT tableA.team1, tableA.team2, tableB.team, tableB.image,
FROM tableA
LEFT JOIN tableB ON tableA.team1=tableB.team
The result only display imageA on the column. Are there any ways to select imageA and image B without using the second query? I appreciate any helps! Thanks a lot!
My table structure are:
table A
team1 team2
------------
AA BB
table B
team image
-------------
AA imagaA
BB imageB
What you do here is called a JOIN (although you do it implicitly because you select from multiple tables). This means, if you didn't put any conditions in your WHERE clause, you had all combinations of those tables. Only with your condition you restrict your join to those rows where the drink id matches.
You could try something like this: SELECT ... FROM ( SELECT f1,f2,f3 FROM table1 UNION SELECT f1,f2,f3 FROM table2 ) WHERE ...
In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables.
That would be something like:
SELECT tableA.team1, tableA.team2, tableB.team, tableB.image, tb.image
FROM tableA
LEFT JOIN tableB ON tableA.team1=tableB.team
LEFT JOIN tableB tb ON tableA.team2=tb.team
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With