从 Table A 中选择所有详细信息,但仅针对存在于 Table B 的前 N 行中的值
Selecting all details from Table A but only for the values that exist in Table B's top N rows
这是使用 Oracle SQL。
编辑了我的代码,我粘贴错了。
所以我有两个 table:class 和事件。每个 class 都有一个 class_id,它也出现在事件 table 中。一些 classes 可以作为多个事件存在。
首先我需要按降价顺序订购 classes,仅使用
"class" table.
然后我需要 select 只有前 4 个最昂贵的 class
之前的结果。
然后我需要显示事件中的所有信息table
只有前 4 个最昂贵的 classes。这并不意味着只有 4 行,因为正如我所说,有些 class 有多个事件。
我设法使前两个要点起作用,但是一旦我将前两个 SELECT 语句嵌套到最后的 SELECT,它就会显示所有 classes,即使是不在前4名的。
SELECT x.*,
c.price,
c.class_name
FROM event x, class c
WHERE EXISTS (SELECT *
FROM (SELECT price, class_name
FROM class
ORDER BY price DESC)
WHERE ROWNUM <=4)
AND c.class_id=x.class_id;
所以 WHERE EXISTS 之后的所有内容都有效,但它就像忽略了 WHERE EXISTS。
那是因为是忽略了exists
。 Exists
returns 真或假(或 NULL)。您的子句通常返回四行。这些存在。所以,一切都会被归还。也许你想要这个:
SELECT x.*, c.price, c.class_name
FROM event x JOIN
(SELECT *
FROM (SELECT class_id, price, class_name
FROM class
ORDER BY price DESC
) c
WHERE ROWNUM <= 4
) c
ON c.class_id = x.class_id;
您应该首先 select 按价格排序的前 4 类,然后在外部 SQL 获取事件数据
select evt.*, top_classes.price, top_classes.class_name
from event evt,
(select class_id, price, class_name
from (select price, class_id, class_name
from class
order by price DESC)
where rownum <=4) top_classes
where evt.class_id = top_classes.class_id;
您的 WHERE EXISTS
子查询中缺少连接。也许把它放在 WITH
子句中会有帮助?
WITH c1 AS ( -- Top 4 prices?
SELECT * FROM (
SELECT price, class_id
FROM class
ORDER BY price DESC
) WHERE rownum <= 4
)
SELECT x.*, c.price, c.class_name
FROM event x, class c
WHERE x.class_id = c.class_id
AND EXISTS ( SELECT 1 FROM c1 WHERE c1.class_id = c.class_id )
但我认为这样做更容易:
WITH c1 AS (
SELECT * FROM (
SELECT class_id, class_name, price
FROM class
ORDER BY price DESC
) WHERE rownum <= 4
)
SELECT x.*, c1.price, c1.class_name
FROM event x, c1
WHERE x.class_id = c1.class_id
希望对您有所帮助。
也许你的意思是:
SELECT x.*,
c.price,
c.class_name
FROM event x, class c
WHERE (c.price,c.class_name) IN (
SELECT price, class_name
FROM (SELECT price, class_name
FROM class
ORDER BY price DESC)
WHERE ROWNUM <=4)
AND c.class_id=x.class_id;
这是使用 Oracle SQL。
编辑了我的代码,我粘贴错了。
所以我有两个 table:class 和事件。每个 class 都有一个 class_id,它也出现在事件 table 中。一些 classes 可以作为多个事件存在。
首先我需要按降价顺序订购 classes,仅使用 "class" table.
然后我需要 select 只有前 4 个最昂贵的 class 之前的结果。
然后我需要显示事件中的所有信息table 只有前 4 个最昂贵的 classes。这并不意味着只有 4 行,因为正如我所说,有些 class 有多个事件。
我设法使前两个要点起作用,但是一旦我将前两个 SELECT 语句嵌套到最后的 SELECT,它就会显示所有 classes,即使是不在前4名的。
SELECT x.*,
c.price,
c.class_name
FROM event x, class c
WHERE EXISTS (SELECT *
FROM (SELECT price, class_name
FROM class
ORDER BY price DESC)
WHERE ROWNUM <=4)
AND c.class_id=x.class_id;
所以 WHERE EXISTS 之后的所有内容都有效,但它就像忽略了 WHERE EXISTS。
那是因为是忽略了exists
。 Exists
returns 真或假(或 NULL)。您的子句通常返回四行。这些存在。所以,一切都会被归还。也许你想要这个:
SELECT x.*, c.price, c.class_name
FROM event x JOIN
(SELECT *
FROM (SELECT class_id, price, class_name
FROM class
ORDER BY price DESC
) c
WHERE ROWNUM <= 4
) c
ON c.class_id = x.class_id;
您应该首先 select 按价格排序的前 4 类,然后在外部 SQL 获取事件数据
select evt.*, top_classes.price, top_classes.class_name
from event evt,
(select class_id, price, class_name
from (select price, class_id, class_name
from class
order by price DESC)
where rownum <=4) top_classes
where evt.class_id = top_classes.class_id;
您的 WHERE EXISTS
子查询中缺少连接。也许把它放在 WITH
子句中会有帮助?
WITH c1 AS ( -- Top 4 prices?
SELECT * FROM (
SELECT price, class_id
FROM class
ORDER BY price DESC
) WHERE rownum <= 4
)
SELECT x.*, c.price, c.class_name
FROM event x, class c
WHERE x.class_id = c.class_id
AND EXISTS ( SELECT 1 FROM c1 WHERE c1.class_id = c.class_id )
但我认为这样做更容易:
WITH c1 AS (
SELECT * FROM (
SELECT class_id, class_name, price
FROM class
ORDER BY price DESC
) WHERE rownum <= 4
)
SELECT x.*, c1.price, c1.class_name
FROM event x, c1
WHERE x.class_id = c1.class_id
希望对您有所帮助。
也许你的意思是:
SELECT x.*,
c.price,
c.class_name
FROM event x, class c
WHERE (c.price,c.class_name) IN (
SELECT price, class_name
FROM (SELECT price, class_name
FROM class
ORDER BY price DESC)
WHERE ROWNUM <=4)
AND c.class_id=x.class_id;