Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL query returning multiple rows based on id

Tags:

sql

postgresql

I am trying to figure out how I can select multiple rows from a table, based on the id column. Something like this:

select * from table where id=1 or id=2 or id=3

Should I loop through every id and perform a query for each iteration?

like image 832
Jen Zhang Avatar asked Aug 26 '13 18:08

Jen Zhang


People also ask

How do I SELECT unique rows in PostgreSQL?

Removing duplicate rows from a query result set in PostgreSQL can be done using the SELECT statement with the DISTINCT clause. It keeps one row for each group of duplicates. The DISTINCT clause can be used for a single column or for a list of columns.

Can we use Rowid in PostgreSQL?

In an Oracle database, the ROWID pseudocolumn is a physical address of a row in a table. This pseudocolumn is used to uniquely identify a row even if the primary key isn't present on a table. PostgreSQL has a similar pseudocolumn called ctid , but it cannot be used as a ROWID .

Is used in PostgreSQL to retrieve rows from more than one table?

Using Joins Joins are used to retrieve rows from two or more tables, based on a related column between those tables.

How do I return a set of records in PostgreSQL?

Let's make a function that returns all the rows of a table whose name you pass in as a parameter. create or replace function GetRows(text) returns setof record as ' declare r record; begin for r in EXECUTE ''select * from '' || $1 loop return next r; end loop; return; end ' language 'plpgsql';


2 Answers

select *
from table
where id in (1, 2, 3)
like image 105
Roman Pekar Avatar answered Oct 18 '22 03:10

Roman Pekar


If you want to have results where id = 1 and results where id = 2 and results where id = 3, you have to use a different logic.

You actually want results where id = 1 or id = 2 or id = 3

Or you want results where id in (1, 2, 3)

Or you want results where id between 1 and 3

like image 8
Yan Brunet Avatar answered Oct 18 '22 04:10

Yan Brunet