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Óï¾ä¾ÍÏԵ÷dz£¼òµ¥ÁË£¬ÈçÏÂËùʾ£º
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´úÂë
¸´ÖÆ´úÂë ´úÂëÈçÏÂ:
Ïà¹ØÎĵµ£º
#Á½ÈÕÆÚ/ʱ¼äÖ®¼äÏà²îµÄÌìÊý£º¡¡
To_Days(end_time)-To_Days(start_time)¡¡
#Á½ÈÕÆÚ/ʱ¼äÖ®¼äÏà²îµÄÃëÊý£º¡¡
SELECT¡¡UNIX_TIMESTAMP(end_time)-¡¡UNIX_TIMESTAMP(start_time);¡¡
#Á½ÈÕÆÚ/ʱ¼äÖ®¼äÏà²îµÄʱ·ÖÊý£º¡¡
SELECT¡¡SEC_TO_TIME(UNIX_TIMESTAMP(end_time)-¡¡UNIX_T ......
Love in coding...
Free and Susan
[Òý]MySQL INNODBÀàÐͱíµÄÍâ¼ü¹ØÁªÉèÖÃ
Here is a simple example that relates parent and child tables through a single-column foreign key:
CREATE TABLE parent (id INT NOT NULL,
......
ÔÚÌÚ¿ÆÅàѵÁËÓÐ1¸ö°ëÔÂÁË£¬ÖÕÓÚѧÍêJSP+SERVLET+JAVABEAN+MYSQL£¬Ñ§ÁËjavaµÄµÚÒ»Ì×web½â¾ö·½°¸¡£ÀÏʦҪÇóÎÒÃÇ6ÈËÒ»×鿪ʼ×öÒ»¸ö¶©²Íϵͳ¡£Õâ¸öϵͳûÓÐÊ²Ã´ÌØ±ðµÄ£¬¾Í¸úµç×ÓÉ̳ÇÊÇÒ»¸öÑùµÄ¡£ÓÉÓÚÒª¶ÔÊý¾Ý¿â×ö´óÁ¿µÄÔöɾ¸Ä²é£¬ÄǸö´úÂëÓֶ࣬ÎÒÏëÄܲ»Äܽ«Êý¾Ý¿â²Ù×÷µÄÕâÒ»²ã´ò°üÆðÀ´(SqlOperDao)£¬ÈÃÎÒ¶ÔÊý¾Ý¿â²Ù×÷£¬Óü¸Ðд ......
ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«Ñ§Ï°ÈçºÎͨ¹ýPHP 5µÄÒ»¸ö»ù±¾³éÏóÀàʹÓÃÍâ¼üÔ¼ÊøÀ´¸üÐÂÁ½¸öInnoDB±íµÄÊý¾Ý¡£ ÎÒÃǽ«Í¨¹ý¾ßÌåʾÀýÀ´ËµÃ÷ÈçºÎͨ¹ý·þÎñÆ÷¶Ë½Å±¾ÓïÑÔÀ´Ê¹ÓÃÍâ¼üÔ¼Êø¡£
¡¡¡¡Ò»¡¢ÀûÓÃÍâ¼üÔ¼Êø¸üÐÂMySQLÖеÄÊý¾Ý
¡¡¡¡ÏÖÔÚ£¬×îÁ÷ÐеĿªÔ´¹ØÏµÐÍÊý¾Ý¿â¹ÜÀíϵͳ·ÇMySQLĪÊô£¬¶øMySQLÓÖÖ§³Ö¶à¸ö´æ´¢ÒýÇæ£¬ÆäÖÐĬÈϵÄÒ²ÊÇËٶȽϿìµÄ´ ......
Each MySQL Cluster host computer running an SQL node must have
installed on it a MySQL binary. For management nodes and data
nodes, it is not necessary to install the MySQL server binary, but
management nodes require the management server daemon
......