一些SQL操作(收集)

1. GROUP_CONCAT

例如:数据如下

+----------+----+--------------------------+
| locus    | id | journal                  |
+----------+----+--------------------------+
| AB086827 |  1 | Unpublished              |
| AB086827 |  2 | Submitted (20-JUN-2002)  |
| AF040764 | 23 | Unpublished              |
| AF040764 | 24 | Submitted (31-DEC-1997)  |
+----------+----+--------------------------+

语句 SELECT locus, GROUP_CONCAT(id) FROM info WHERE locus IN('AB086827','AF040764') GROUP BY locus; 的返回结果为

+----------+------------------+
| locus    | GROUP_CONCAT(id) |
+----------+------------------+
| AB086827 | 1,2              |
| AF040764 | 23,24            |
+----------+------------------+

2. FIND_IN_SET(str,strlist)函数

功能:查询字段 (strlist) 中包含(str)的结果,返回结果为null或记录

参数:1) str 要查询的字符串(可以是个字段名)

2) strlist 字段名 参数以 ”,” 分隔 如 (1, 2, 6, 8)

示例:1) SELECT FIND_IN_SET('b', 'a,b,c,d');   // 返回值为2,即第2个值

2) SELECT * FROM t_areainfo WHERE FIND_IN_SET(id,strlist);  // 输出表中find_in_set返回的哪些记录

 

3. 数组 函数:

 implode()  ---把数组转换成字符串  
explode()  ---把字符串转换成数组

 

4. SELECT INTO 语法

1)  语句从一个表中选取数据,然后把数据插入另一个表中。

SELECT *

INTO  new_table_name [IN externaldatabase]

FROM  old_tablename

 

2)  选择的结果处理后存入变量

SELECT  group_concat(id) INTO  sTempChd  FROM  treeNodes  where

FIND_IN_SET(pid, sTempChd) > 0;

  # 对于所有sTempChd中的父节点,找出其子节点,并将它们串接,存入STempChd

 

5. SQL将查询结果插入到另一个表的三种情况

1)如果要插入目标表不存在:

select * into 目标表 from 表 where ...

2)如果要插入目标表已经存在:

insert into 目的表 select * from 表 where 条件

3)如果是跨数据库操作的话: 怎么把A数据库的atable表所查询的东西,全部插入到B 数据库的btable表中

select * into B.btable from A.atable where ..

 

posted @ 2017-09-03 07:40  carol_wei  阅读(178)  评论(0编辑  收藏  举报