W
W
www32017-02-18 13:33:39
PostgreSQL
www3, 2017-02-18 13:33:39

How to produce correct SQL result?

Good afternoon! There is a table product(id, option) and it looks something like this
id | option
1 | 1
1 | 2
2 | 1
2 | 3
3 | 2
...
How to form a request in such a way as to display, for example, a product with options 1, 2?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey S., 2017-02-18
@www3

SELECT id FROM  T  where o =1 and id in (select id from t where o=2)

option two:
select id from (
  select id, max(o1) as o1, max(o2) as o2 from (
      select id,1 as o1, 0 as o2 from T where o = 1
    union
      select id,0 as o1, 1 as o2 from T where o = 2
  ) as T1 group by id
) as T2
where o1=1 and o2=1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question