这是我的MySQL表的快照:
是否可以编写查询来获得像输出这样的数据透视表...
答案 0 :(得分:3)
像
这样的东西SELECT url_host,
SUM(CASE WHEN post_id = -1 THEN 1 ELSE 0 END) as post_id_minus1,
SUM(CASE WHEN post_id = 0 THEN 1 ELSE 0 END) as post_id_0,
etc...
FROM YOUR_TABLE
GROUP BY url_host
答案 1 :(得分:2)
您可以使用CASE
语句来转动您的表格。
SELECT url_host,
COUNT(CASE WHEN post_ID = -1 THEN 1 ELSE NULL END) Negative_One,
COUNT(CASE WHEN post_ID = 0 THEN 1 ELSE NULL END) Zero,
COUNT(CASE WHEN post_ID > 0 THEN 1 ELSE NULL END) Greater_Zero
FROM tableName
GROUP BY url_host