mysql 高级查询

1. 分组字段由多行合并为一行。

mysql> select user_id,tag_id from yyfax_user.user_tag where user_id='U000412280';
+------------+----------+
| user_id    | tag_id   |
+------------+----------+
| U000412280 | TAG20000 |
| U000412280 | TAG20002 |
| U000412280 | TAG50001 |
+------------+----------+
3 rows in set (0.00 sec)
mysql> select user_id,GROUP_CONCAT(tag_id SEPARATOR ',') AS tag_id from yyfax_user.user_tag where user_id='U000412280';
+------------+----------------------------+
| user_id    | tag_id                     |
+------------+----------------------------+
| U000412280 | TAG20000,TAG20002,TAG50001 |
+------------+----------------------------+
1 row in set (0.00 sec)

 

2. 将查询结果按照字段指定的顺序排序。

mysql> select user_id,create_time from yyfax_user.user_info where user_id in ('400','600','100');
+---------+---------------------+
| user_id | create_time         |
+---------+---------------------+
| 100     | 2014-10-08 08:58:58 |
| 400     | 2014-10-09 15:34:02 |
| 600     | 2014-10-10 11:26:10 |
+---------+---------------------+
3 rows in set (0.00 sec)
mysql> select user_id,create_time from yyfax_user.user_info where user_id in ('400','600','100') order by field (user_id,'400','600','100');
+---------+---------------------+
| user_id | create_time         |
+---------+---------------------+
| 400     | 2014-10-09 15:34:02 |
| 600     | 2014-10-10 11:26:10 |
| 100     | 2014-10-08 08:58:58 |
+---------+---------------------+
3 rows in set (0.01 sec)

 

上一篇:调用支付JSAPI缺少参数:total_fee解决方法


下一篇:获取其它小程序的appid和path