我尝试了下面的查询,并返回了错误“single-row query returns more than one row
”
SELECT (SELECT COUNT(*) FROM pans) AS NUMBEROFPANS,
(SELECT name FROM facts, pans WHERE fact_id=pafact_id) AS NAMEOFPANS,
(SELECT COUNT(*) FROM stas) AS NUMBEROFSTAS,
(SELECT name FROM facts, stas WHERE fact_id=safact_id) AS NAMEOFSTAS,
(SELECT COUNT(*) FROM zelyc) AS NUMBEROFZELYCS,
(SELECT name FROM facts, Zelycs WHERE fact_id=zafact_id) AS NAMEOFZELYCS
FROM DUAL;
期望的结果:
Numberofpans Nameofpans Numberofstas Nameofstas
1 jade 1 relate
1 arrow 1 simi
我正在使用oracle
并且pans,stas和zelycs是事实表的子类,因此它们都具有相同的主键 是否有不同的方式来编写此查询?
答案 0 :(得分:1)
您可以运行每个子查询以查找返回多行的那个。
或者,您可以添加&{39; where rownum = 1
'到每个子查询,强制它只返回它来的第一行。
答案 1 :(得分:0)
查询
SELECT COUNT(*) ...
将始终返回1行,因此它们没问题。
你有3个'细节'表格平底锅,stas和zelyc 在其中一个中,fact_id有多行。 这是一个查找所有有问题的fact_id的查询:
SELECT 'pans' tbl, pafact_id, count(*) FROM pans
group by pafact_id
having count(*) > 1
union all
SELECT 'stas' tbl, pafact_id, count(*) FROM stas
group by pafact_id
having count(*) > 1
union all
SELECT 'zelyc' tbl, pafact_id, count(*) FROM zelyc
group by pafact_id
having count(*) > 1
所以如何计算多个表中多列返回多行
我不是在跟踪.. 您不能从SELECT和FROM关键字之间的子查询返回数据集(多个行/列)。此子查询应该是标量并且只返回一个值。
答案 2 :(得分:0)
我没有统计所有表中的数据,而是将每个表分别加入到其超类中以获取数据的数量和数据的名称
SELECT
COUNT(fact_id) As NUMBEROFPANS,
name AS NAMEOFPANS
FROM
facts,
pans
WHERE
fact_id=pafact_id GROUP BY (name);
输出:
NUMBEROFPANS NAMEOFPANS
1 Jade
1 arrow
所以我为所有表格做了同样的事情
答案 3 :(得分:0)
您在帖子中尝试做的事情并不是很清楚。我想,考虑到你对其他答案的其他答案/评论,这可能就是你想要做的事情:
SELECT
Count(p.pafact_id) as PanCount,
p.name as Pan,
Count(s.safact_id) as StasCount,
s.name as Stas,
Count(z.zafact_id) as ZelycsCount,
z.name as Zelyc
FROM
facts f
LEFT JOIN pans p
ON f.fact_id = pafact_id
LEFT JOIN stas s
ON f.fact_id = safact_id
LEFT JOIN zelycs z
ON f.fact_id = zafact_id
GROUP BY
p.name,
s.name,
z.name