发布网友 发布时间:2022-03-24 22:03
共6个回答
热心网友 时间:2022-03-24 23:32
select
goods_name 产品名字,
max(price) 价格,
max(case rank_name when 'VIP1' then (case when user_price is null then discount*price/100 else user_price end) end) vip1价格,
max(case rank_name when 'VIP2' then (case when user_price is null then discount*price/100 else user_price end) end) vip2价格,
max(case rank_name when 'VIP3' then (case when user_price is null then discount*price/100 else user_price end) end) vip3价格
from member_price mp
left join goods gd on gd.goods_id = mp.goods_id
left join user_rank ur on ur.rank_id = mp.rank_id
group by goods_name;
mysql不是很熟悉,所以写了个标准sql语法的。我记得mysql中group by的select可以不用跟聚合函数的,所以LZ大概可以把max()去掉,而mysql中好像有isnull,ifnull的函数,我不太了解,这里用case语句替代了,LZ可以自行替换。
热心网友 时间:2022-03-25 00:50
select
a.goods_name,
a.price,
ifnull(b.user_price,(select discount from user_rank where rank_name='VIP1')*a.price/100) as vip1,
ifnull(c.user_price,(select discount from user_rank where rank_name='VIP2')*a.price/100) as vip2,
ifnull(d.user_price,(select discount from user_rank where rank_name='VIP3')*a.price/100) as vip3
from
goods a
left join member_price b on a.goods_id=b.goods_id and b.rank_id=(select rank_id from user_rank where rank_name='VIP1')
left join member_price c on a.goods_id=c.goods_id and c.rank_id=(select rank_id from user_rank where rank_name='VIP2')
left join member_price d on a.goods_id=d.goods_id and d.rank_id=(select rank_id from user_rank where rank_name='VIP3')追问for the right syntax to use near 'IFNULL(b.user_price,(select discount from `qinndb`.`ecs_user_rank` where rank_' at line 1
)
[3] => Array
(
[errno] => 10
)
)
热心网友 时间:2022-03-25 02:25
select
t1.goods_name 产品名字, max(price) 价格,
max(case when t2.rank_id=1 then (case when t3.user_price is not null then t3.user_price else t2.discount*t1.price/100 end) end) VIP1价格,
max(case when t2.rank_id=2 then (case when t3.user_price is not null then t3.user_price else t2.discount*t1.price/100 end) end) VIP2价格,
max(case when t2.rank_id=3 then (case when t3.user_price is not null then t3.user_price else t2.discount*t1.price/100 end) end) VIP3价格
from goods t1 cross join user_rank t2
left join member_price t3
on t1.goods_id=t3.goods_id and t2.rank_id=t3.rank_id
group by t1.goods_id, t1.goods_name
热心网友 时间:2022-03-25 04:16
你这个member_price 表示不是设计有问题,要想显示 产品名字 价格 vip1价格 VIP2价格 VIP3价格 级应该设计为 price_id, goods_id, user_price ,vip1_price,vip2_price,vip3_price!追问我也觉得设计不好,这是二次开发,不想再修改表
热心网友 时间:2022-03-25 06:24
用 case when 判断即可!
热心网友 时间:2022-03-25 08:49
mysql是数据库,建议你去看一下有关mysql的书籍或者视频教程。