mysql方法來源於:http://www.cnblogs.com/jjcc/p/5896588.htmlhtml
###在網上看到一篇,很是讚的方法###mysql
好比說要獲取班級的前3名,mysql就能夠用GROUP_CONCAT + GROUP BY + substring_index實現。sql
考試表函數
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
`id` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`score` int(11) DEFAULT NULL,
`class` char(12) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 工具
插入數據spa
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('1', 'Bobdd', '25', '1');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('2', 'xx', '20', '2');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('3', 'Jack', '30', '2');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('4', 'Bill', '32', '4');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('5', 'Nick', '22', '3');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('6', 'Kathy', '18', '3');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('7', 'Steve', '36', '3');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('8', 'Anne', '25', '2');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('9', 'Kathy', '18', '2');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('11', 'Bob1', '25', '3');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('12', 'Jane1', '20', '1');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('13', 'Jack1', '30', '1');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('14', 'Bill1', '32', '1');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('15', 'Nick1', '22', '4');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('16', 'Kathy1', '18', '4');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('17', 'Steve1', '36', '4');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('18', 'Anne1', '25', '1');
INSERT INTO `test` (`id`, `name`, `score`, `class`) VALUES ('19', 'Kathy1', '18', '2');htm
運用group_concat + GROUP BY 分組 獲取前3名blog
select GROUP_CONCAT(t1.id) as ids from (
SELECT t.class, substring_index(GROUP_CONCAT(t.id ORDER BY t.score desc),',',3) as id from
test t GROUP BY t.class
)t1排序
獲得ip
注意 是t.id ORDER BY t.score desc 分數從高到低。
上面的語句只是獲取到總的id。可是轉換爲列不太好弄。能夠拆分用union all 來搞。
獲取第一名
SELECT t.class, substring_index(GROUP_CONCAT(t.id ORDER BY t.score desc),',',1) as id from
test t GROUP BY t.class
union all
-- 第二名
SELECT t.class, substring_index(substring_index(GROUP_CONCAT(t.id ORDER BY t.score desc),',',2),',',-1) as id from
test t GROUP BY t.class
union all
-- 第三名
SELECT t.class, substring_index(substring_index(GROUP_CONCAT(t.id ORDER BY t.score desc),',',3),',',-1) as id from
test t GROUP BY t.class
好了到如今 已經獲取到了一個list
用 in 來完成最後的步驟
SELECT class,score,name FROM test where id in(
SELECT id from
(SELECT t.class, substring_index(GROUP_CONCAT(t.id ORDER BY t.score desc),',',1) as id from
test t GROUP BY t.class
union all
SELECT t.class, substring_index(substring_index(GROUP_CONCAT(t.id ORDER BY t.score desc),',',2),',',-1) as id from
test t GROUP BY t.class
union all
SELECT t.class, substring_index(substring_index(GROUP_CONCAT(t.id ORDER BY t.score desc),',',3),',',-1) as id from
test t GROUP BY t.class) t2
) ORDER BY class asc,score desc
最終結果
關於mysql函數GROUP_CONCAT
GROUP_CONCAT([DISTINCT] expr [,expr ...] [ORDER BY {unsigned_integer | col_name | expr} [ASC | DESC] [,col_name ...]] [SEPARATOR str_val])
1.例如:
SELECT student_id, GROUP_CONCAT(courses_id) AS courses FROM student_courses WHERE student_id=2 GROUP BY student_id;
+------------+---------+
| student_id | courses |
+------------+---------+
| 2 | 3,4,5 |
+------------+---------+
2.固然分隔符還能夠自定義,默認是以「,」做爲分隔符,若要改成「|||」,則使用SEPARATOR來指定,例如:
SELECT student_id, GROUP_CONCAT(courses_id SEPARATOR '|||') AS courses FROM student_courses WHERE student_id=2 GROUP BY student_id;
+------------+---------+
| student_id | courses |
+------------+---------+
| 2 | 3|||4|||5 |
+------------+---------+
3.除此以外,還能夠對這個組的值來進行排序再鏈接成字符串,例如按courses_id降序來排:
SELECT student_id, GROUP_CONCAT(courses_id ORDER BY courses_id DESC) AS courses FROM student_courses WHERE student_id=2 GROUP BY student_id;
+------------+---------+
| student_id | courses |
+------------+---------+
| 2 | 5,4,3 |
+------------+---------+
4.須要注意的:
a.int字段的鏈接陷阱
當你用group_concat的時候請注意,鏈接起來的字段若是是int型,必定要轉換成char再拼起來,
不然在你執行後(ExecuteScalar或者其它任何執行SQL返回結果的方法)返回的將不是一個逗號隔開的串,
而是byte[]。
該問題當你在SQLyog等一些工具中是體現不出來的,因此很難發現。
select group_concat(ipaddress) from t_ip 返回逗號隔開的串
select group_concat(id) from t_ip 返回byte[]
select group_concat(CAST(id as char)) from t_dep 返回逗號隔開的串
select group_concat(Convert(id , char)) from t_dep 返回逗號隔開的串
附Cast,convert的用法:
CAST(expr AS type), CONVERT(expr,type) , CONVERT(expr USING transcoding_name)
CAST() 和CONVERT() 函數可用來獲取一個類型的值,併產生另外一個類型的值。
這個類型 能夠是如下值其中的 一個:
BINARY[(N)]
CHAR[(N)]
DATE
DATETIME
DECIMAL
SIGNED [INTEGER]
TIME
UNSIGNED [INTEGER]
b.長度陷阱
用了group_concat後,select裏若是使用了limit是不起做用的.
用group_concat鏈接字段的時候是有長度限制的,並非有多少連多少。但你能夠設置一下。
使用group_concat_max_len系統變量,你能夠設置容許的最大長度。
程序中進行這項操做的語法以下,其中 val 是一個無符號整數:
SET [SESSION | GLOBAL] group_concat_max_len = val;
若已經設置了最大長度,則結果被截至這個最大長度。
在SQLyog中執行 SET GLOBAL group_concat_max_len = 10 後,從新打開SQLyog,設置就會生效。
SELECT SUBSTRING(str,pos,len)函數用法
SELECT SUBSTRING(str,pos,len) 返回字符串 str 中以 pos 做爲起始位置,長度爲 len 的子字符串。
SELECT SUBSTRING(str FROM pos FOR len) -- 返回字符串 str 中以 pos 做爲起始位置,長度爲 len 的子字符串。
SELECT SUBSTRING(str FROM pos) -- 返回字符串 str 中以 pos 做爲起始位置,到結束的子字符串。
SELECT SUBSTRING(str,pos) -- 返回字符串 str 中以 pos 做爲起始位置,到結束的子字符串。
SELECT SUBSTRING(str,pos,len) -- 返回字符串 str 中以 pos 做爲起始位置,長度爲 len 的子字符串。
應用實例:
SELECT SUBSTRING('123456' FROM 2 FOR 3)
------------------------------------------
234
SELECT SUBSTRING('123456',2,3)
--------------------------------------
234
SELECT SUBSTRING('123456' FROM 2)
-----------------------------------
23456
SELECT SUBSTRING('123456',2)-----------------------------------23456