简体   繁体   English

SQL:具有10个字段的表,其中包含来自其他表的ID或0(内部或左连接以“读取”表?)

[英]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. 我有11个表的数据库。

The tables A contains 15 fields, 10 of theirs contain an id from another table or the 0 value. tables A包含15个字段,其中的10个包含来自另一个表的ID或0值。 Each of these tables has two fields, id and description . 每个表都有两个字段, iddescription

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. 我想查询数据库以获得在table A ID处具有正确descriptiontable A ;如果ID为0,则返回null。

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: 使用此语句选择描述并使用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 当不可能加入时,您将得到Null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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