mysql - My sql results from rows to column -
this question has answer here:
- mysql pivot table 8 answers
is there way insert values of count(*) these columns. explain. have table
+--------+----------+ | url | type | +--------+----------+ | url1 | | +--------+----------+ | url1 | b | +--------+----------+ | url2 | c | +--------+----------+ | url2 | c | +--------+----------+
where values how many times type (a,b,c) appears in data set (this why want count) want have @ final table
+--------+------+-----+-----+ | url | | b | c | +--------+------+-----+-----+ | url1 | 3 | 1 | 0 | +--------+------+-----+-----+ | url2 | 2 | 0 | 1 | +--------+------+-----+-----+
until did this
select url, type, count(*) table group url,type
and i'm getting (random example)
|url |type |count of specific type url| https://1 1 https://2 1 https://2 b 1 https://3 b 1 https://4 c 4 https://4 b 2 https://4 b 2
and want insert values table above
you use case when , group by
select url, sum(case when type ='a' 1 else 0 end) a, sum(case when type ='b' 1 else 0 end) b, sum(case when type ='c' 1 else 0 end) c my_table group url ;
Comments
Post a Comment