2017-11-17 101 views
0

我使用SQL對帕拉如何選擇下一列時,前一列滿足使用SQL

一定的條件,我查詢的表格看起來像

客戶名稱shop1 shop1number SHOP2 shop2number shop3 shop3number

TOM AB 111 AA 231 321 AC

AMY AC 121 AB 213 AD 231

弗蘭克AD AE 123 233 234 AB

enter image description here 這裏,數字是客戶忠誠度數字,如果我在尋找店鋪1(AB)的忠誠度數字,我不知道客戶填寫忠誠度時,號,這是他們的選擇就擺在不惜一切爲了他們個人資料

+0

請您可以使表格設計更清晰一點?也許提供一張圖片? – majjam

+0

這是3排嗎? '店1/AAA /店鋪2/BBB /店鋪3/CCC'或在同一行上的單個列? –

+0

沒有那是一排有6列,因此該表看起來像 –

回答

0

如果我理解正確的話,你正在尋找一個店鋪相關的所有忠誠編號的數字,所以一個方法可以把行數據列第一使用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 | 
+--------------+------+------------+ 

DEMO

相關問題