如果我理解正确的话,您将查找与商店相关联的所有忠诚度数字,因此一种方法是首先使用 union all 然后找一家商店;让我们说 AB .
select * from
(
select customername, shop1 as shop, shop1number as shopnumber
from table1
union all
select customername, shop2 as shop, shop2number as shopnumber
from table1
union all
select customername, shop3 as shop, shop3number as shopnumber
from table1
) t
where t.shop = 'AB';
结果:
+--------------+------+------------+
| customername | shop | shopnumber |
+--------------+------+------------+
| AMY | AB | 213 |
| TOM | AB | 111 |
| Franck | AB | 234 |
+--------------+------+------------+
declare @shop varchar(10)
set @shop='AB'
select cname,
case when shop1=@shop then shop1
when shop2=@shop then shop2
when shop3=@shop then shop3
end
as shop,
shop1number as shopnumber
from tblcus
2条答案
按热度按时间3ks5zfa01#
如果我理解正确的话,您将查找与商店相关联的所有忠诚度数字,因此一种方法是首先使用
union all
然后找一家商店;让我们说AB
.结果:
演示
yqyhoc1h2#