|
我有一个只有两列的表格$ s6 q9 Q. o' e& e# V
mysql> select * from stuff_table;8 A" _7 G% Z2 j3 V( M1 r, H
+------+-------+! H5 w8 w B4 }/ _ ^7 h
| name | stuff |
/ u1 `' l5 n7 Y+------+-------+
2 D& s& A' x) I+ v| John | shoes |
7 [' l- D7 ~$ B* |# _% u5 O3 P| Jim | bag |
* n0 ?# \3 y. [. f| Ely | book |9 \2 k5 N }# C* |
| Tom | bag |
4 E% j6 x& t3 O: n. H) c| Will | shoes |+ K" B n7 w: g6 B" r* @1 i0 X8 B
| Pitt | book |
; k: Q; y! m/ a' d: M| Dean | bag |2 C6 h* Z* d8 w* G+ }
| Luke | bag |
0 S4 v( v3 j4 H1 k! f+------+-------+
0 m" i, [% ~$ C+ r! }1 |' C& z$ g我尝试了很多发现的解决方案,例如8 V/ k2 e5 p# \& ]7 W0 ^
select distinct
/ u5 y6 u+ E7 ]" g6 {8 d) a) {$ cmax(case when stuff='shoes' then name else name is null) end as shoes,
$ G: ~8 i5 y% U) Zmax(case when stuff='bag' then name else name is null end) as bag,
( u: u& F% ~0 t, @% Z+ M& N @2 dmax(case when stuff='book' then name else name is null end) as book5 s" h& x, I Q: h$ y* z
from stuff_table;0 x" a) q, ^/ L9 N
但是我刚得到这个: z0 |! e( i# e- _; x# O' p( \1 ~6 N
+-------+------+------+
" V [2 x, Q/ s4 w$ C| shoes | bag | book |$ ~" O1 R: d4 O
+-------+------+------+
$ k* R5 U8 U5 Q( g| Will | Tom | Pitt |* X% o- _+ @' ]: ^
+-------+------+------+6 K. b! R" F9 s: D9 j8 r. \
相反,我想得到这个1 n6 A$ Q' }7 w+ [. t1 u
+-------+------+------+
" Q3 J0 Q" y6 E: n* w! q| shoes | bag | book |
* K3 {! \& X. G, U s% p+-------+------+------+0 U9 P0 f" A3 o1 q6 T& Z
| John | Jim | Ely |) j& ]4 V# [; o" m2 n1 [* A
| Will | Tom | Pitt |
& r8 Q- L' J" w* z| NULL | Dean | NULL |
+ g$ m: Z. k) ?| NULL | Luke | NULL |
4 s$ P$ q/ Z: ~5 V7 d+-------+------+------+3 v! a0 B! v; m# u3 k( V
我已经尝试过sum(case …)或if(case ..)或group by,但是它不起作用。是否有任何mysql查询来获得该表?请帮助。谢谢你。
: r4 U! \. S2 r0 N3 Z% M
' g& Y' U$ @9 }, r解决方案:; N$ _* g" h% D% p* p n
, [; g9 K# A% G S2 T
9 s% m7 V; s7 `; Z0 i3 i6 b7 i( T" A7 k6 u: f" e% p
根据mysql您使用的版本,这是一种建立row_number每个组,然后conditional aggregation按该行号分组的方法:) Y$ F- u# n: F, M( E+ Y
select : a i1 p! @1 A
rn,
7 I7 x$ s1 s* i& x max(case when stuff = 'bag' then name end) 'bag',; K# R% Z* b1 }5 ~$ k0 }
max(case when stuff = 'book' then name end) 'book',
$ u5 H8 `1 Y! q* |4 ]) ?, Y max(case when stuff = 'shoes' then name end) 'shoes'
3 n8 N) e5 p5 @from (2 p4 A( B: w1 G; n* b, ~' T ]
select *, row_number() over (partition by stuff order by name) rn
3 y) B* J4 T7 g( m- q from stuff_table9 ?: A2 T/ F/ R2 O8 e. \+ h
) t% e5 d6 z8 u D
group by rn
2 z3 A0 \; ~& `0 L小提琴演示2 h j8 |) q5 A
2 ?. R( a- f3 [5 V6 E! b; N/ x2 a由于您使用的是旧版本mysql,因此您需要使用user-defined variables来建立行号。其余的工作原理相同。这是一个例子:6 V/ Y# H# o+ K
select
) W( t D2 `+ X rn, 0 v* D' ^8 k% A/ X0 w' G. I
max(case when stuff = 'bag' then name end) 'bag',; P. W! y* V; E+ s' o
max(case when stuff = 'book' then name end) 'book',/ m; Y! S6 \9 Y4 Y2 f$ L
max(case when stuff = 'shoes' then name end) 'shoes'
3 Y7 \ F+ {3 I) Ufrom (
5 s, n' A/ f# u" J9 [( Z select *,
* N+ D5 X& b, x9 e9 R% n1 \ ( case stuff
/ {5 T6 X h+ Y0 c3 ^& S( I6 x when @curStuff6 n1 Z8 _" j% L/ D8 u
then @curRow := @curRow + 1
; Q7 v/ j' J& a9 N4 M! ?0 y( j else @curRow := 1 and @curStuff := stuff 5 x( J- s) F8 C6 n
end
+ d$ O# j N2 s r' u ) + 1 AS rn \- ~ C0 M# E8 C& R9 E
from stuff_table, (select @curRow := 0, @curStuff := '') r
# L% ?* Q; E: V# R4 X3 U! }9 Z order by stuff: ^0 s8 ]0 q; }
) t
" x- p1 Q D0 R; i6 wgroup by rn |
|