简体   繁体   中英

Query to get data from two tables

I'm trying to get data from two MySQL tables, but having problems.

Usually, I'd use join to get data from two those tables depending on data, but this time I cannot, I think.

Here's situation: I'm having tables photos and photos_albums.

photos ID ALBUM FILENAME
photos_albums ID TITLE NAME

I need to get all albums and photo if exist in photos table. When I use join query will return albums only if photo with specific album id exists.

How to resolve? Any suggestions? Any help appreciated.

Regards, Tom

Probalby you are using INNER JOIN (or just JOIN , which is the same), this way you are given the rows that have data in both tables. Try so select from albums and LEFT JOIN with photos table.

When joining two tables, you can specify the join type in order to return records that have matching criteria (in the ON clause), or all records from one of the join tables, regardless if it has a corresponding row in the other table. This can be achieved by using a LEFT OUTER JOIN .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM