0

http://i.stack.imgur.com/mbUTI.jpg

我想在一个查询中进行多项选择,其中一个表包含来自多个表的数据。我有四个表将其组合成一个输出。这是我的桌子的图像。

我想全选,不希望其他表中的其他数据只是带有所有其他表名称的主表

已尝试关注但它不起作用。

    select * from project_content 
    left Join project_master on project_master.id =  project_content.p_id
    left Join project_content_menu on project_content_menu.type_id =  project_content.p_c_id 
    left Join project_menu_master on project_menu_master.id =  project_content.m_id 


    select * from project_content 
    left Join project_master on project_master.id =  project_content.p_id
    left Join project_content_menu on project_content_menu.type_id =  project_content.p_c_id 
    left Join project_menu_master on project_menu_master.id =  project_content.m_id 


    select distinct(*) from project_content 
    left OUTER Join   project_master on project_master.id =  project_content.p_id
    left OUTER Join   project_content_menu on project_content_menu.type_id =  project_content.p_c_id 
    left OUTER Join  project_menu_master on project_menu_master.id =  project_content.m_id 

    select * from project_content ,project_master,project_content_menu,project_menu_master
    where project_master.id =  project_content.p_id and project_content_menu.type_id =  project_content.p_c_id and project_menu_master.id =  project_content.m_id 

    select pc.id as id , pm.name as pname , pmm.name as menuname , pcm.name as contentname , pc.name as name
    from 
    project_content as pc,
    project_master as pm,
    project_content_menu as pcm,
    project_menu_master as pmm
    where 
    pm.id = pc.p_id 
    and 
    pcm.type_id = pc.m_id
    and 
    pmm.id = pc.p_c_id
4

3 回答 3

0

我不是 SQL 专家,但您可以尝试使用UNION运算符。像这样:

select names
from table1
where lastname like k%
UNION
select names
from table2
where lastname like k%  

这将结合 table1 和 table2 的结果,并在结果中显示 UNIQUE NAMES,其中姓氏以 k 开头。所以,如果有 JOHN KRAMER 和 JOHN KUTCHER,那么只有 JOHN 会显示一次。
如果您也想要重复条目,请使用UNION ALL

于 2012-06-04T12:56:32.797 回答
0

我不确定,我是否很好地理解了您的问题,但是如果您需要将所有记录添加在一起,您应该使用:UNION ALL

像:

select name from project_content

UNION ALL

select name from project_master 

UNION ALL

select name from project_master 

UNION ALL

select name from project_menu_master 

请确保您在每个选择中具有相同类型的相同数量的列

于 2012-06-04T12:59:01.150 回答
0

如果我没有正确地理解你,你想要只来自主表的数据,但使用名称而不是来自其他表的外键?如果是这样,那么:

SELECT pc.id, pm.name, pcm.name, pmm.name, pc.name, pc.desc, pc.thumb, pc.src, pc.status
FROM project_content AS pc
LEFT JOIN project_master AS pm ON pm.id =  pc.p_id
LEFT JOIN project_content_menu AS pcm ON pcm.type_id = pc.p_c_id 
LEFT JOIN project_menu_master AS pmm ON pmm.id = pc.m_id 
于 2012-06-04T13:08:47.877 回答