Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a constant in Postgresql

Tags:

postgresql

Suppose that I have this query:

select * 
from myTable
where myTable.myCol in (1,2,3)

I would like to do that:

with allowed_values as (1,2,3)
select * 
from myTable
where myTable.myCol in allowed_values

It gives me a Syntax Error in the first row, can you help me fixing it?

like image 675
Aslan986 Avatar asked Jun 10 '26 17:06

Aslan986


1 Answers

The closest I can think to your syntax:

WITH allowed_values (id) AS 
  ( VALUES
      (1), (2), (3)
  )
SELECT * 
FROM myTable
WHERE id IN 
   (TABLE allowed_values) ;

Tested in SQL-Fiddle

like image 178
ypercubeᵀᴹ Avatar answered Jun 13 '26 08:06

ypercubeᵀᴹ