简体   繁体   中英

SQL: WITH clause how to refer to column_name

I will create some pseudo code to explain my problem:

WITH MAX_BES AS 
(SELECT MAX(ID) AS MAX_ID FROM TABLE1);

SELECT .....
FROM 
  TABLE2, TABLE3
WHERE
  TABLE2.ID IN (MAX_BES.MAX_ID);

The code above does not work. How should I reference a column from my WITH clause within an IN clause?

Thanks a lot for your quick feedback.

Use a sub-select and make that one statement, not two.

WITH max_bes AS (
  SELECT max(id) AS max_id 
  FROM table1
) --<< no ; here!
SELECT .....
FROM table2
  JOIN table3 ON ... ??
WHERE table2.id IN (SELECT max_id FROM max_bes);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM