ITeye博客

GROUP_CONCAT() ,CAST() 和CONVERT()和一个mysql面试题

yangze · 2010-10-15 10:37 · [网站分类]MySQL · Markdown

GROUP_CONCAT()是MySQL数据库提供的一个函数,通常跟GROUP BY一起用,具体可参考MySQL官方文挡:http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_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 |
+------------+---------+ 
这 就不需要用php循环了
 $row = $pdo->query("SELECT student_id, GROUP_CONCAT(courses_id) AS courses FROM student_courses WHERE student_id=2 GROUP BY student_id");
 $result = explode(',', $row['courses']); 

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,设置就会生效。

 

下面这个面试题就用到了GROUP_CONCAT()

 

他们公司网站上的广告位是轮播的,每天某一广告位最多可轮播的广告数量是有限制的,比如A广告位,每天只能轮播三个广告,但销售人员在销售广告位时并不考虑此限制,要求查询出合同表中,超过广告位轮播数量的合同。
mysql> select * from s1;
+----+----+------------+------------+
| t1 | t2 | t3 | t4 |
+----+----+------------+------------+
| 1 | A | 2006-11-01 | 2006-11-03 |
| 2 | C | 2006-11-02 | 2006-11-03 |
| 3 | B | 2006-11-01 | 2006-11-04 |
| 4 | A | 2006-11-03 | 2006-11-04 |
| 5 | C | 2006-11-01 | 2006-11-02 |
| 6 | B | 2006-11-02 | 2006-11-05 |
| 7 | A | 2006-11-02 | 2006-11-03 |
| 8 | A | 2006-11-04 | 2006-11-05 |
| 9 | C | 2006-11-03 | 2006-11-04 |
| 10 | C | 2006-11-02 | 2006-11-04 |
+----+----+------------+------------+
mysql> select * from s2;
+----+----+
| t6 | t5 |
+----+----+
| A | 2 |
| B | 1 |
| C | 3 |
+----+----+
8 rows in s
广告位A每天最多可轮播2个广告,但合同表中在2006-11-03这天有三个广告(1、4、7),对于广告位A,1、4、7则是最终需要得到的结果。如需要可使用临时表、存储过程等。
最终得到
超过相应t5的值
t2 adate value
A 2006-11-01 1
A 2006-11-02 1,7

 

解答:

建立一个日历表 calendar 放上一段期间内的日期

SQL code

mysql> select * from s1;
+----+------+------------+------------+
| t1 | t2   | t3         | t4         |
+----+------+------------+------------+
|  1 | A    | 2006-11-01 | 2006-11-03 |
|  2 | C    | 2006-11-02 | 2006-11-03 |
|  3 | B    | 2006-11-01 | 2006-11-04 |
|  4 | A    | 2006-11-03 | 2006-11-04 |
|  5 | C    | 2006-11-01 | 2006-11-02 |
|  6 | B    | 2006-11-02 | 2006-11-05 |
|  7 | A    | 2006-11-02 | 2006-11-03 |
|  8 | A    | 2006-11-04 | 2006-11-05 |
|  9 | C    | 2006-11-03 | 2006-11-04 |
| 10 | C    | 2006-11-02 | 2006-11-04 |
+----+------+------------+------------+
10 rows in set (0.00 sec)

mysql> create table calendar(d date primary key);
Query OK, 0 rows affected (0.08 sec)

mysql> insert into calendar values
    -> ('2006-11-01'),
    -> ('2006-11-02'),
    -> ('2006-11-03'),
    -> ('2006-11-04'),
    -> ('2006-11-05'),
    -> ('2006-11-06'),
    -> ('2006-11-07'),
    -> ('2006-11-08'),
    -> ('2006-11-09'),
    -> ('2006-11-10'),
    -> ('2006-11-11'),
    -> ('2006-11-12'),
    -> ('2006-11-13'),
    -> ('2006-11-14'),
    -> ('2006-11-15'),
    -> ('2006-11-16'),
    -> ('2006-11-17'),
    -> ('2006-11-18'),
    -> ('2006-11-19'),
    -> ('2006-11-20');
Query OK, 20 rows affected (0.06 sec)
Records: 20  Duplicates: 0  Warnings: 0

mysql>
mysql> select s1.t2,c.d,GROUP_CONCAT(s1.t1)
    -> from calendar c ,s1
    -> where c.d between s1.t3 and s1.t4
    -> group by s1.t2,c.d;
+------+------------+---------------------+
| t2   | d          | GROUP_CONCAT(s1.t1) |
+------+------------+---------------------+
| A    | 2006-11-01 | 1                   |
| A    | 2006-11-02 | 7,1                 |
| A    | 2006-11-03 | 7,1,4               |
| A    | 2006-11-04 | 8,4                 |
| A    | 2006-11-05 | 8                   |
| B    | 2006-11-01 | 3                   |
| B    | 2006-11-02 | 6,3                 |
| B    | 2006-11-03 | 6,3                 |
| B    | 2006-11-04 | 6,3                 |
| B    | 2006-11-05 | 6                   |
| C    | 2006-11-01 | 5                   |
| C    | 2006-11-02 | 5,10,2              |
| C    | 2006-11-03 | 9,10,2              |
| C    | 2006-11-04 | 9,10                |
+------+------------+---------------------+
14 rows in set (0.00 sec)

mysql>

注:以上资源来源于网络

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。