我的一張表裡有三個字段,id,a,b。id字段不是唯一的,a字段裡有RE開頭和RG開頭的值,b字段是它對應的結果,有0和1兩種。我現在想寫sql語句,查詢結果為,以RE和RG開頭的兩種值分成兩個字段RE.a和RG.a。如圖所示:
SELECT
rg.id,
re.a,
re.b,
rg.a,
rg.b
FROM
(SELECT
t1.*
FROM
test t1
WHERE t1.a
LIKE 'RE%') re
RIGHT JOIN
(SELECT
*
FROM
test t2
WHERE t2.a
LIKE 'RG%') rg
ON re.id = rg.id
AND SUBSTRING(re.a, 3) = SUBSTRING(rg.a, 3);