所以我有两个表,customer_special
和customer_nuk
都包含完全相同的列。
我有另一张表customer_data
,其编号为id
我希望运行一个查询,其结果包含来自customer_special
和customer_nuk
的所有条目,使用customer_special.user_id
列和customer_nuk.user_id
列中的值{{1}来自inner join
的结果。
对于此示例,分别从customer_data
和id
的每一行返回customer_data
只需返回customer_special
。我尝试过各种各样的东西,但这些都不是我所希望的。
以下按预期工作;内部将customer_nuk
连接到customer_data
的正确行但是它没有按要求引用这两个表。
customer_special
然后:
SELECT
t.order_id as "Order ID", t.BY1 as "By", t.order_type as "Type", t.O_DATE as "Order Date",
tr.short_name as "Customer short name", tr.www as "Customer website",
tr.owner as "Customer owner", tr.postcode as "Customer postcode"
FROM customer_special t
INNER JOIN customer_data AS tr
ON t.customer = tr.id
LIMIT 50
是我最近的尝试。
任何有关应该是一项简单任务的帮助都将受到赞赏。感谢信。
答案 0 :(得分:1)
如果您希望来自两个相同表的所有数据与第三个表连接,那么您正在寻找的是union
,它将从两个相同的表中生成一组数据并将该集合与第三表:
select * from (
select * from customer_special
union all
select * from customer_nuk
) src
join customer_data on src.user_id = customer_data.id
请注意,使用union all
不会消除任何重复的行。
*
应该替换为每个表所需的列,并且对于联合中的两个表都必须相同。