create table t(a int, b int); insert into t values (1,1),(1,2),(1,3),(2,1),(2,2),(2,3),(3,1),(3,2),(3,3); select * from t; a | b ---------- 1 | 1 1 | 2 1 | 3 2 | 1 2 | 2 2 | 3 3 | 1 3 | 2 3 | 3 select max(case when a = 1 then b else 0 end) as q, max(case when b = 1 then a else 0 end) as c, (max(case when a = 1 then b else 0 end)+max(case when b = 1 then a else 0 end)) as x from t
是否有可能做這樣的事情?如何獲得價值x沒有代碼重複
select max(case when a = 1 then b else 0 end) as q, max(case when b = 1 then a else 0 end) as c, (q + c) as x from t
1)答案可能會因您的SQL供應商。 MySQL的?甲骨文? SQL Server? 2)我下注了一個臨時表,它包含前兩列,然後從中選擇兩列加上新列,可以做到 – Patashu 2013-05-01 08:51:11