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
答案 0 :(得分:0)
我不是SQL专家,但您可以尝试使用UNION
运算符。像这样:
select names
from table1
where lastname like k%
UNION
select names
from table2
where lastname like k%
这将结合table1和table2的结果,并在lastname以k开头的结果中显示UNIQUE NAMES。所以,如果有一个JOHN KRAMER和JOHN KUTCHER那么只有JOHN会显示一次。
如果您也想要重复输入,请使用UNION ALL
答案 1 :(得分: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
请确保,每个选择中具有相同数量的列具有相同的类型
答案 2 :(得分: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