简体   繁体   中英

SQL: table with 10 fields which contain ids from other tables or 0 (inner or left join to “read” the table?)

I have a db with 11 tables.

The tables A contains 15 fields, 10 of theirs contain an id from another table or the 0 value. Each of these tables has two fields, id and description .

I would to query the db to obtain the table A with the right description at the place of the id or null if the id is 0.

What do I have to use? join, left or inner join? how?

use this statement to select description & use left join of rest of the tables with tablea:

IF(id = 0, NULL, description) 

Example:

SELECT A.* , IF(A.bid = 0, NULL, B.description) , IF(A.cid = 0, NULL, C.description) 
from tablea A 
LEFT JOIN tableb B on A.bid = B.id 
LEFT JOIN tablec C on A.cid = C.id 

and so on....

Left join

SELECT * FROM A 
    LEFT JOIN B ON (A.KEY=B.KEY)

and you get Null when the join isn't possible

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