每个人我都有问题。我有两个查询,运行良好分开。但是我想在我尝试的一个查询中进行,但那些不起作用。我需要加入两个输出。
首先查询:
SELECT q_internal_table.q_part_id,
q_external_table.q_external_id,
q_external_table.q_external_approve,
q_external_table.q_order_id,
q_internal_table.q_internal_id,
q_internal_table.q_internal_approve
FROM (SELECT parts.id AS q_part_id,
parts.order_id AS q_order_id,
external_reports.id AS q_external_id,
external_reports.approved AS q_external_approve
FROM parts
INNER JOIN external_reports
ON( parts.id = external_reports.part_id ))
q_external_table
INNER JOIN (SELECT parts.id AS q_part_id,
internal_reports.id AS q_internal_id,
internal_reports.approved AS q_internal_approve
FROM parts
INNER JOIN internal_reports
ON( parts.id = internal_reports.part_id ))
q_internal_table
ON( q_external_table.q_part_id = q_internal_table.q_part_id )
WHERE ( q_external_table.q_external_approve = 'Y'
OR q_internal_table.q_internal_approve = 'Y' )
第二次查询:
SELECT q_five_internal_table.q_five_part_id,
q_five_internal_table.q_five_internal_id,
q_five_internal_table.q_five_internal_approve,
q_five_external_table.q_five_external_id,
q_five_external_table.q_five_external_approve,
q_five_external_table.q_five_order_id
FROM (SELECT parts.id AS q_five_part_id,
parts.order_id AS q_five_order_id,
five_way_external_reports.id AS q_five_external_id,
five_way_external_reports.approved AS q_five_external_approve
FROM parts
INNER JOIN five_way_external_reports
ON( parts.id = five_way_external_reports.part_id ))
q_five_external_table
INNER JOIN (SELECT parts.id AS q_five_part_id,
five_way_internal_reports.id AS q_five_internal_id,
five_way_internal_reports.approved AS q_five_internal_approve
FROM parts
INNER JOIN five_way_internal_reports
ON( parts.id = five_way_internal_reports.part_id ))
q_five_internal_table
ON( q_five_external_table.q_five_part_id = q_five_internal_table.q_five_part_id )
WHERE ( q_five_external_table.q_five_external_approve = 'Y'
OR q_five_internal_table.q_five_internal_approve = 'Y' )
我试过了这个问题: -
SELECT q_internal_external_table.*,
q_five_internal_external_table.*
FROM (SELECT q_internal_table.q_part_id,
q_external_table.q_external_id,
q_external_table.q_external_approve,
q_external_table.q_order_id,
q_internal_table.q_internal_id,
q_internal_table.q_internal_approve
FROM (SELECT parts.id AS q_part_id,
parts.order_id AS q_order_id,
external_reports.id AS q_external_id,
external_reports.approved AS q_external_approve
FROM parts
INNER JOIN external_reports
ON( parts.id = external_reports.part_id ))
q_external_table
INNER JOIN (SELECT parts.id AS q_part_id,
internal_reports.id AS q_internal_id,
internal_reports.approved AS q_internal_approve
FROM parts
INNER JOIN internal_reports
ON( parts.id = internal_reports.part_id ))
q_internal_table
ON( q_external_table.q_part_id = q_internal_table.q_part_id )
WHERE ( q_external_table.q_external_approve = 'Y'
OR q_internal_table.q_internal_approve = 'Y' ))
q_internal_external_table
INNER JOIN (SELECT q_five_internal_table.q_five_part_id,
q_five_internal_table.q_five_internal_id,
q_five_internal_table.q_five_internal_approve,
q_five_external_table.q_five_order_id,
q_five_external_table.q_five_external_id,
q_five_external_table.q_five_external_approve
FROM (SELECT parts.id AS q_five_part_id,
parts.order_id AS
q_five_order_id
,
five_way_external_reports.id AS
q_five_external_id,
five_way_external_reports.approved AS
q_five_external_approve
FROM parts
INNER JOIN five_way_external_reports
ON( parts.id = five_way_external_reports.part_id ))
q_five_external_table
INNER JOIN (SELECT parts.id AS q_five_part_id,
five_way_internal_reports.id AS
q_five_internal_id,
five_way_internal_reports.approved AS
q_five_internal_approve
FROM parts
INNER JOIN five_way_internal_reports
ON( parts.id = five_way_internal_reports.part_id ))
q_five_internal_table
ON ( q_five_external_table.q_five_part_id =
q_five_internal_table.q_five_part_id )
WHERE ( q_five_external_table.q_five_external_approve = 'Y'
OR q_five_internal_table.q_five_internal_approve = 'Y' ))
q_five_internal_external_table
ON ( q_internal_external_table.q_part_id =
q_five_internal_external_table.q_five_part_id )
QUERY结果: -
在此查询中有三个结果。但我想在第一个查询结果中得到7结果。这三个答案已经在第一个查询结果中。我也需要休息4。我认为问题出在ON q_internal_external_table.q_part_id = q_five_internal_external_table.q_five_part_id
。但不知道如何解决它。
请帮帮我。提前谢谢。
答案 0 :(得分:0)
您需要的是UNION
。
两个查询都需要具有相同数量的列和类型。在您的情况下,您需要更改所选列的顺序,以便生成UNION
。
应该是这样的:
SELECT q_internal_table.q_part_id,
q_external_table.q_external_id,
q_external_table.q_external_approve,
q_external_table.q_order_id,
q_internal_table.q_internal_id,
q_internal_table.q_internal_approve
FROM (SELECT parts.id AS q_part_id,
parts.order_id AS q_order_id,
external_reports.id AS q_external_id,
external_reports.approved AS q_external_approve
FROM parts
INNER JOIN external_reports
ON( parts.id = external_reports.part_id ))
q_external_table
INNER JOIN (SELECT parts.id AS q_part_id,
internal_reports.id AS q_internal_id,
internal_reports.approved AS q_internal_approve
FROM parts
INNER JOIN internal_reports
ON( parts.id = internal_reports.part_id ))
q_internal_table
ON( q_external_table.q_part_id = q_internal_table.q_part_id )
WHERE ( q_external_table.q_external_approve = 'Y'
OR q_internal_table.q_internal_approve = 'Y' )
UNION ALL
SELECT q_five_internal_table.q_five_part_id,
q_five_external_table.q_five_external_id,
q_five_external_table.q_five_external_approve,
q_five_external_table.q_five_order_id,
q_five_internal_table.q_five_internal_id,
q_five_internal_table.q_five_internal_approve
FROM (SELECT parts.id AS q_five_part_id,
parts.order_id AS q_five_order_id,
five_way_external_reports.id AS q_five_external_id,
five_way_external_reports.approved AS q_five_external_approve
FROM parts
INNER JOIN five_way_external_reports
ON( parts.id = five_way_external_reports.part_id ))
q_five_external_table
INNER JOIN (SELECT parts.id AS q_five_part_id,
five_way_internal_reports.id AS q_five_internal_id,
five_way_internal_reports.approved AS q_five_internal_approve
FROM parts
INNER JOIN five_way_internal_reports
ON( parts.id = five_way_internal_reports.part_id ))
q_five_internal_table
ON( q_five_external_table.q_five_part_id = q_five_internal_table.q_five_part_id )
WHERE ( q_five_external_table.q_five_external_approve = 'Y'
OR q_five_internal_table.q_five_internal_approve = 'Y' )
注意我已经更改了第二个查询中列的顺序以匹配第一个查询的类型。
答案 1 :(得分:0)
如果我们假设查询的最终目标是在下表中找到parts.id,那么您更新的查询似乎是正确的(语义上): external_reports,internal_reports,five_way_external_reports,five_way_internal_reports
在组合查询1和查询2的行时将使用Union。
但是,如果您要查找匹配的列(join子句),则使用JOIN而不是UNION。
另外,请为您的查询尝试使用EXPLAIN和EXPLAIN EXTENDED前缀来理解查询计划。
答案 2 :(得分:0)
特别感谢@ dnoeth。伟大的人。
修改后的SQL是,
select q_internal_external_table.*,q_five_internal_external_table.* from
(select q_internal_table.q_part_id,
q_external_table.q_external_id,q_external_table.q_external_approve,
q_external_table.q_order_id,q_internal_table.q_internal_id,
q_internal_table.q_internal_approve from(SELECT parts.id as
q_part_id,parts.order_id as q_order_id,external_reports.id as
q_external_id,external_reports.approved as q_external_approve from parts
LEFT join external_reports on(parts.id=external_reports.part_id))
q_external_table LEFT join (SELECT parts.id as q_part_id,internal_reports.id
as q_internal_id,internal_reports.approved as q_internal_approve from parts
LEFT join internal_reports on(parts.id=internal_reports.part_id))
q_internal_table on(q_external_table.q_part_id=q_internal_table.q_part_id)
where (q_external_table.q_external_approve='Y' or
q_internal_table.q_internal_approve='Y')) q_internal_external_table LEFT
join (select q_five_internal_table.q_five_part_id,
q_five_internal_table.q_five_internal_id,
q_five_internal_table.q_five_internal_approve,
q_five_external_table.q_five_order_id,
q_five_external_table.q_five_external_id,
q_five_external_table.q_five_external_approve from (SELECT parts.id as
q_five_part_id,parts.order_id as q_five_order_id,
five_way_external_reports.id as q_five_external_id,
five_way_external_reports.approved as q_five_external_approve from parts
LEFT join five_way_external_reports on(parts.id=
five_way_external_reports.part_id)) q_five_external_table LEFT join (SELECT
parts.id as q_five_part_id,five_way_internal_reports.id as
q_five_internal_id,five_way_internal_reports.approved as
q_five_internal_approve from parts LEFT join five_way_internal_reports
on(parts.id=five_way_internal_reports.part_id)) q_five_internal_table on
(q_five_external_table.q_five_part_id=q_five_internal_table.q_five_part_id)
where (q_five_external_table.q_five_external_approve='Y' or
q_five_internal_table.q_five_internal_approve='Y'))
q_five_internal_external_table on (q_internal_external_table.q_part_id
=q_five_internal_external_table.q_five_part_id)