如何将 select 语句与另外两个 select 语句的联合

how to join a select statement with the union of two other select statements

table_a:

id 描述 类别
0 “等等等等” 类别 1
1 “很多话” 类别 1
2 “有用的描述” 类别 1
3 “非常高兴” 类别 1
4 “5 颗星” 类别 1
5 “不错” 类别 1
6 “不太好” 类别 1
7 “干得好” 类别 1
8 “印象不深” 类别 2
9 “还好” 类别 2

table_b:

id 数量 person_id 类型
0 000 00000000 b
1 100 11111111 b
2 200 22222222 b
3 300 33333333 b

table_c:

id 数量 person_id 类型
4 400 44444444 c
5 500 55555555 c
6 600 66666666 c
7 700 77777777 c

期望:

id 数量 person_id 类型 描述
0 000 00000000 b “等等等等”
1 100 11111111 b “很多话”
2 200 22222222 b “有用的描述”
3 300 33333333 b “非常高兴”
4 400 44444444 c “5 颗星”
5 500 55555555 c “不错”
6 600 66666666 c “不太好”
7 700 77777777 c “干得好”

以下在仅包含 t1 的行中给出了“SQL 命令未正确结束”。我知道这个错误与我如何为 table 添加别名有关,但我不确定下一步该怎么做。

SELECT 
    id, description
FROM 
    table_a
WHERE category = 'Category 1'
t1

LEFT JOIN

(
SELECT 
    id, num, person_id, type
FROM 
    table_b
UNION
SELECT 
    id, num, person_id, type
FROM 
    table_c
) t2
ON 
    t1.id = t2.id

此查询应该符合您的预期

SELECT   t1.id, t2.num, t2.person_id, t2.type, t2.description
FROM     table_a t1
LEFT JOIN 
(
    SELECT  id, num, person_id, type
    FROM    table_b
    UNION
    SELECT id, num, person_id, type
    from   table_c
) t2 on t1.id = t2.id
    
WHERE category = 'Category 1'

两个选项要么在子查询中使用 where 子句,要么在连接条件之后使用它。

SELECT 
    id, description
FROM 
    table_a
t1

LEFT JOIN

(
SELECT 
    id, num, person_id, type
FROM 
    table_b
UNION
SELECT 
    id, num, person_id, type
FROM 
    table_c
) t2
ON 
    t1.id = t2.id
WHERE t1.category = 'Category 1'

select * from (SELECT 
    id, description
FROM 
    table_a WHERE category = 'Category 1')
t1

LEFT JOIN

(
SELECT 
    id, num, person_id, type
FROM 
    table_b
UNION
SELECT 
    id, num, person_id, type
FROM 
    table_c
) t2
ON 
    t1.id = t2.id
WHERE t1.category = 'Category 1'