gpt4 book ai didi

sql - 跨行查询条件

转载 作者:行者123 更新时间:2023-11-29 11:28:54 25 4
gpt4 key购买 nike

我有一张这样的 table

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

在这个表中,我有一些重复的条目,因为我需要存储 vip 价格。如果是普通人访问,我需要出示

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

如果vip人来了,我应该显示价格

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

最后一列是 bool 值。

我需要查询来获取项目。请帮助获取对此的查询。

最佳答案

普通客户:

select item, price
from tablename
where vip is NULL

贵宾客户:

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)

关于sql - 跨行查询条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31262840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com