想要将第二个查询的列连接到第一个查询但出现 "query block has incorrect number of result columns" 等错误
Want to concatenate column of the second query to the first query but getting errors such as "query block has incorrect number of result columns"
SELECT
ID, PRIM_EMAIL, SEC_EMAIL, PHONE
FROM
STUDENTS.RECORDS
WHERE
ID IN (SELECT ID FROM STUDENTS.INFO WHERE ROLL_NO = '554')
UNION
SELECT NAME
FROM STUDENTS.INFO
WHERE ROLL_NO = '554';
这里 Roll_No
是用户插入的数据,所以现在我已经对其进行了硬编码。基本上在 ROLL_NO
的帮助下,我从获得 ID
的地方对 STUDENTS_INFO
table 进行排序,并在此基础上尝试获得 PRIM_EMAIL
、SEC_EMAIL
, PHONE
来自 STUDENTS.RECORDS
table 同时匹配两个 table 的外键。除了当前结果集,我还想要 prov_name 列。
非常感谢任何帮助。谢谢!
我怀疑您想将所有这些信息放在同一行,这表明 join
而不是 union all
:
select
r.ID,
r.PRIM_EMAIL,
r.SEC_EMAIL,
r.PHONE,
r.NAME
from STUDENTS.RECORDS r
inner join STUDENTS.INFO i ON i.ID = r.ID
where I.ROLL_NO = '554';
我认为您的错误 query block has incorrect number of result columns
的根源在于尝试将具有 4 列 (id, prim_email, sec_email, phone
) 和 1 列 (name
) 的 table 合并在一起.
根据你的问题,我了解到你想要 students.records
的 id, prim_email, sec_email, phone
和 students.info
的 name
中的单个 table。
我认为以下使用 CTE 的查询可能会让您(部分)获得最终结果。您可能需要重构以优化性能。
with s_records as ( select * from students.records ),
s_info as ( select * from students.info ),
joined as (
select
s_records.id,
s_records.prim_email,
s_records.sec_email,
s_records.phone,
s_info.name
from s_records
left join s_info
on s_records.roll_no = s_info.roll_no
where roll_np = '554' )
select * from joined
总的来说,我认为 join
将成为您解决方案的一部分,而不是 union
:-)
SELECT
ID, PRIM_EMAIL, SEC_EMAIL, PHONE
FROM
STUDENTS.RECORDS
WHERE
ID IN (SELECT ID FROM STUDENTS.INFO WHERE ROLL_NO = '554')
UNION
SELECT NAME
FROM STUDENTS.INFO
WHERE ROLL_NO = '554';
这里 Roll_No
是用户插入的数据,所以现在我已经对其进行了硬编码。基本上在 ROLL_NO
的帮助下,我从获得 ID
的地方对 STUDENTS_INFO
table 进行排序,并在此基础上尝试获得 PRIM_EMAIL
、SEC_EMAIL
, PHONE
来自 STUDENTS.RECORDS
table 同时匹配两个 table 的外键。除了当前结果集,我还想要 prov_name 列。
非常感谢任何帮助。谢谢!
我怀疑您想将所有这些信息放在同一行,这表明 join
而不是 union all
:
select
r.ID,
r.PRIM_EMAIL,
r.SEC_EMAIL,
r.PHONE,
r.NAME
from STUDENTS.RECORDS r
inner join STUDENTS.INFO i ON i.ID = r.ID
where I.ROLL_NO = '554';
我认为您的错误 query block has incorrect number of result columns
的根源在于尝试将具有 4 列 (id, prim_email, sec_email, phone
) 和 1 列 (name
) 的 table 合并在一起.
根据你的问题,我了解到你想要 students.records
的 id, prim_email, sec_email, phone
和 students.info
的 name
中的单个 table。
我认为以下使用 CTE 的查询可能会让您(部分)获得最终结果。您可能需要重构以优化性能。
with s_records as ( select * from students.records ),
s_info as ( select * from students.info ),
joined as (
select
s_records.id,
s_records.prim_email,
s_records.sec_email,
s_records.phone,
s_info.name
from s_records
left join s_info
on s_records.roll_no = s_info.roll_no
where roll_np = '554' )
select * from joined
总的来说,我认为 join
将成为您解决方案的一部分,而不是 union
:-)