Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL select two tables at the same time

Tags:

command

php

mysql

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
like image 486
FlyingCat Avatar asked May 19 '10 01:05

FlyingCat


People also ask

Can you SELECT from 2 tables in MySQL?

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.

How can I fetch data from two tables in MySQL without joining?

You could try something like this: SELECT ... FROM ( SELECT f1,f2,f3 FROM table1 UNION SELECT f1,f2,f3 FROM table2 ) WHERE ...

Can you SELECT 2 tables in SQL?

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.


1 Answers

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
like image 100
jeroen Avatar answered Sep 26 '22 00:09

jeroen