Home > Enterprise >  How to select many record from database in Select range?
How to select many record from database in Select range?

Time:04-28

I have query

select `id`, `name` from `dishes` where id IN (5,5,5)

How to select many items with 5 id?

id name
5  dish
5  dish
5 dish

I got a response response

CodePudding user response:

You can use where id = 5 : all rows that match will be returned.
NB id is usually a PRIMARY KEY and UNIQUE which means that it will only match one row.

select `id`, `name` from `dishes` where id = 5;

if you want to use the syntax in then just put 5 once.

select `id`, `name` from `dishes` where id IN (5);

CodePudding user response:

Following your comment I think that you are looking for a join with multiple where conditions.
To code this properly the 2 id columns would be primary key and the dish_id column would be a foreign key.

create table dishes (id int, name varchar(20));
insert into dishes values(1,'Potato salad');
create table orders(id int, client varchar(10), dish_id int);
insert into orders values(1,'Mr Jones',1);
select 
  o.id,
  o.client,
  d.id,
  d.name
from orders o
join dishes d
on o.dish_id = d.id
where o.id = 1 
and d.id = 1 ;
id | client   | id | name        
-: | :------- | -: | :-----------
 1 | Mr Jones |  1 | Potato salad

db<>fiddle here

  • Related