作者grassboy2 (天才小胖子-活力花俏草儿)
看板Database
标题Re: [SQL ] mysql语法问题
时间Mon Feb 5 17:14:05 2007
※ 引述《Suzanne (漫。舞。细。雨)》之铭言:
: 我有一个资料表
: order_id status day
: ==================================
: 10001 A 2005-06-01
: 10002 B 2005-06-25
: 10003 A 2005-06-30
: 10004 C 2005-07-20
: 10005 A 2005-07-23
: 10006 B 2005-07-30
: sql语法要怎麽select才能整理出下面的表格
: 月份 status=A笔数 status=B笔数 status=C笔数
: ===================================================
: 2005-06 2 1 0
: 2005-07 1 1 1
: 用我呆呆的脑袋只想出
: $str = "select SUBSTRING(day,1,7),count(status) as a from orders
: where status='A' group by SUBSTRING(day,1,7) ";
: 可是只能整理出一种status
: 请问各位高手该怎麽写呢?
我也来呆呆的继续想下去…
SELECT `tmp1`.* , `tmp2`.`B`, `tmp3`.`C` FROM (
SELECT SUBSTRING(day,1,7) AS `month`, COUNT(status) AS `A`
FROM orders WHERE status='A' GROUP BY SUBSTRING(day,1,7)
) AS `tmp1` LEFT JOIN (
SELECT SUBSTRING(day,1,7) AS `month` COUNT(status) AS `B`
FROM orders WHERE status='B' GROUP BY SUBSTRING(day,1,7)
) AS `tmp2` ON `tmp1`.`month` = `tmp2`.`month` LEFT JOIN(
SELECT SUBSTRING(day,1,7) AS `month` COUNT(status) AS `C`
FROM orders WHERE status='C' GROUP BY SUBSTRING(day,1,7)
) AS `tmp3` ON `tmp1`.`month` = `tmp3`.`month`
耶…这是暴力法啦XD
不过多亏这个问题…我才发现原来MySQL有SUBSTRING()
感激不尽m(_ _)m
--
完蛋…连我都不知道这个对不对了><~有错请指正
--
╔作者 grassboy2 来自 ptt.cc 的怒吼!
█ ◎P_GrassboyWebBBS版【http://WebGrassboy.twbbs.org】
Design By Grassboy@MDS 白日梦工作室 ╚─你现在还以为,浏览器不能打逼吗?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.115.214.146
※ 编辑: grassboy2 来自: 140.115.214.146 (02/05 17:21)
※ 编辑: grassboy2 来自: 140.115.214.146 (02/05 17:22)