易截截图软件、单文件、免安装、纯绿色、仅160KB

Mysql的GROUP_CONCAT()函数使用方法 多行合并函数

GROUP_CONCAT 是一个合并多行的函数,一般用的少,所以很多人都不知道,我也是一个,今天偶然看到,故记下方法;
GROUP_CONCAT的语法如下:
GROUP_CONCAT([DISTINCT] expr [,expr ...][ORDER BY {unsigned_integer | col_name | expr}[ASC | DESC] [,col_name ...]][SEPARATOR str_val])
下面演示一下这个函数,先建立一个学生选课表student_courses,并填充一些测试数据。
SQL代码
复制代码 代码如下:
CREATE TABLE student_courses (
student_id INT UNSIGNED NOT NULL,
courses_id INT UNSIGNED NOT NULL,
KEY(student_id)
);
INSERT INTO student_courses VALUES (1, 1), (1, 2), (2, 3), (2, 4), (2, 5);
若要查找学生ID为2所选的课程,则使用下面这条SQL:
SQL代码
复制代码 代码如下:
mysql> SELECT student_id, courses_id from student_courses WHERE student_id=2;
+------------+------------+
| student_id | courses_id |
+------------+------------+
| 2 | 3 |
| 2 | 4 |
| 2 | 5 |
+------------+------------+
3 rows IN SET (0.00 sec)
输出结果有3条记录,说明学生ID为2的学生选了3、4、5这3门课程。
放在PHP里,必须用一个循环才能取到这3条记录,如下所示:
PHP代码
复制代码 代码如下:
foreach ($pdo->query("SELECT student_id, courses_id from student_courses WHERE student_id=2") as $row) {
$result[] = $row['courses_id'];
}
而如果采用GROUP_CONCAT()函数和GROUP BY语句就显得非常简单了,如下所示:
SQL代码
复制代码 代码如下:
mysql> 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 |
+------------+---------+
1 row IN SET (0.00 sec)
这样php里处理就简单了:
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']);
分隔符还可以自定义,默认是以“,”作为分隔符,若要改为“|||”,则使用SEPARATOR来指定,例如:
SQL代码
复制代码 代码如下:


相关文档:

让memcached和mysql更好的工作

 这次是Fotolog
的经验,传说中比Flickr更大的网站
,Fotolog在21台服务
器上部署了51个memcached实例,总计有254G缓存空间
可用,缓存了多达175G的内容,这个数量比很多网站的数据库都要大的多,原文是A Bunch of Great Strategies for Using Memcached and MySQL Better Together
,我这里还是选择性的翻译以及按照 ......

mysql 时间函数

里是一个使用日期函数的例子。下面的查询选择了所有记录,其date_col的值是在最后30天以内: 
  mysql> SELECT something from table 
  WHERE TO_DAYS(NOW()) - TO_DAYS(date_col) <= 30; 
   select   TO_DAYS(NOW());
   select now() ;
  DAYOFWEEK( ......

VC++ 连接MySQL 数据库

把mysql.h复制到vc的目录的include目录下
mysql.h在你mysql的安装目录下的include里面如:mysql\include
把libmysql.lib(在mysql的安装目录下,搜索下就能找到)复制到这个目录下(C:\Program Files\Microsoft Visual Studio 9.0\VC\lib),要不连接会出错。
如果编译连接时还是出错。就把libmysql.lib复制到你源程序的目 ......

MySQL Cluster Multi Computer Configuration


For our four-node, four-host MySQL Cluster, it is necessary to
write four configuration files, one per node host.

Each data node or SQL node requires a
my.cnf
file that provides two pieces of
information: a connectstring
that tells
......

Initial Startup of MySQL Cluster


Starting the cluster is not very difficult after it has been configured. Each cluster node process must be started separately, and on the host where it resides. The management node should be started first, followed by the data nodes, and then finally by any SQL nodes:
On the management host, issu ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号