Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query for conditions across rows

I have a table like this

id |   item |  price | vip
---+--------+--------+-----
0  |  tv    | 2000   | NULL
1  |  tv    | 2500   | TRUE
2  |  camera| 3000   | NULL
3  |  camera| 3500   | TRUE
4  |  phone | 1000   | NULL
5  |  pen   | 2      | NULL

In this table, I have some duplicated entries because I need to store vip price. If normal person visit, I need to show

item  | price
------+---------
tv    | 2000
camera| 3000
phone | 1000
pen   | 2

If vip person comes, then I should show price like

item  |  price
------+-----------
tv    | 2500
camera| 3500
phone | 1000
pen   | 2

The last column is Boolean.

I need query to get items. Please help to get the query for this.

like image 893
Narendra Sisodiya Avatar asked Sep 28 '22 10:09

Narendra Sisodiya


1 Answers

Regular customer:

select item, price
from tablename
where vip is NULL

vip customer:

select item, price
from tablename t1
where vip is true
   or not exists (select 1 from tablename t2
                  where t1.item = t2.item
                    and vip is true)
like image 76
jarlh Avatar answered Nov 15 '22 12:11

jarlh