带有数据透视表的MySQL外连接

时间:2018-04-15 08:02:13

标签: mysql

int 21

我想获取附加到实体的文件和文件夹列表。尝试过以下查询,但如果文件夹或文件中没有记录,它会给出空记录

CREATE TABLE link (
    entityid integer,
    fileid INT (11) NULL,
    folderid INT (11) NULL
);

CREATE TABLE file (
    fileid integer,
    name varchar
);    

CREATE TABLE folder (
    folderid integer,
    name varchar
);

示例数据

select fi.name,fo.name 
FROM link ll
LEFT OUTER JOIN file fi ON lk.FileId = fi.FileId  
LEFT OUTER JOIN folder fo ON lk.FileId = fo.FileId  
WHERE dl.entityid = 307

1 个答案:

答案 0 :(得分:0)

只需将entityid添加到select子句:

SELECT ll.entityid, fi.name, fo.name 
FROM link ll
LEFT OUTER JOIN file fi ON lk.FileId = fi.FileId  
LEFT OUTER JOIN folder fo ON lk.FileId = fo.FileId  
WHERE dl.entityid = 307;