《基础语法篇》增删改查

insert

3种基础insert
insert into 表名 values(值1,值2,......);
insert into 表名(列1,列2,......)values(值1,值2,......);
insert into 表2 select * from 表1;
把查询结果导入到另外一张表中
-- 把A表的全部字段数据插入到B表中(AB表结构相同):
insert into B select * from A;
-- 把A表中某些字段的数据插入B表中:
insert into B(字段名)(select 字段名 from A)

delete

update

更新多个字段

(1)

-- 一般会这样写
update student
  set sname = '李四',
	  sage = 20,
	  sbirthday = to_date('2010-01-01', 'yyyy-mm-dd'),
	  saddress = '广州市越秀区'
where sid = 1;

(2)

-- 如果字段非常多
update student
  set (sname, sage, sbirthday, saddress) =
  (select '李四', 20, to_date('2010-01-01', 'yyyy-mm-dd'), '广州市越秀区' from dual)
where sid = 1;
根据一张表更新另一张表

1.游标

declare
cursor t1 is select * from tablename;
begin
for rec in t1 loop
update tablename t set t.detail=rec.jieshao where t.objectid=rec.objid;
end loop;
end;
update   student   set   (name,id   )=
(select   name   ,id     from   (select   student.rowid   rd,student1.name,student1.id   from   student1,student   where   student1.int_id   =student.int_id)   tmp
where   student.rowid=tmp.rd);
commit;

3.exits

update test_a a set (a.name,a.age)=
(select b.name,b.age from test_b b where a.id = b.id) where exists
(select * from test_b c where c.id=a.id)
UPDATE   t_A   SET   Djrq=
(
SELECT   djrq   FROM   t_B   WHERE   t_A.ID   =   T_B.ID
WHERE   ROWNUM   =   1
)
WHERE   t_A.ID   IN
(
SELECT   ID   FROM   t_B   WHERE   jwh='XX村'
)
update tbl1 a   set (a.col1, a.col2) = (select b.col1, b.col2
from tbl2 b
where a.key = b.key)   where a.key in(select key from tbl2)

select

表关联

join,把来自两个或多个表的行结合起来

下图展示了 LEFT JOIN、RIGHT JOIN、INNER JOIN、OUTER JOIN 相关的 7 种用法。
image

演示如下
在本教程中,我们将使用 RUNOOB 样本数据库。
下面是选自 "Websites" 表的数据:

+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |
| 3  | 菜鸟教程      | http://www.runoob.com/    | 4689  | CN      |
| 4  | 微博          | http://weibo.com/         | 20    | CN      |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
| 7  | stackoverflow | http://stackoverflow.com/ |   0 | IND     |
+----+---------------+---------------------------+-------+---------+

下面是 "access_log" 网站访问记录表的数据:

mysql> SELECT * FROM access_log;
+-----+---------+-------+------------+
| aid | site_id | count | date       |
+-----+---------+-------+------------+
|   1 |       1 |    45 | 2016-05-10 |
|   2 |       3 |   100 | 2016-05-13 |
|   3 |       1 |   230 | 2016-05-14 |
|   4 |       2 |    10 | 2016-05-14 |
|   5 |       5 |   205 | 2016-05-14 |
|   6 |       4 |    13 | 2016-05-15 |
|   7 |       3 |   220 | 2016-05-15 |
|   8 |       5 |   545 | 2016-05-16 |
|   9 |       3 |   201 | 2016-05-17 |
+-----+---------+-------+------------+
9 rows in set (0.00 sec)

请注意,"Websites" 表中的 "id" 列指向 "access_log" 表中的字段 "site_id"。上面这两个表是通过 "site_id" 列联系起来的。
然后,如果我们运行下面的 SQL 语句(包含 INNER JOIN):

SELECT Websites.id, Websites.name, access_log.count, access_log.date
FROM Websites
INNER JOIN access_log
ON Websites.id=access_log.site_id;

结果如下:
image

INNER JOIN:如果表中有至少一个匹配,则返回行
LEFT JOIN:即使右表中没有匹配,也从左表返回所有的行
RIGHT JOIN:即使左表中没有匹配,也从右表返回所有的行
FULL JOIN:只要其中一个表中存在匹配,则返回行
union、union all,合并多个 SELECT 语句的结果集

请注意,UNION 内部的每个 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每个 SELECT 语句中的列的顺序必须相同。
注释:UNION 结果集中的列名总是等于 UNION 中第一个 SELECT 语句中的列名。

  • union,只有不同的值
  • union all,所有值

如下:
下面是选自 "Websites" 表的数据:

mysql> SELECT * FROM Websites;
+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |
| 3  | 菜鸟教程      | http://www.runoob.com/    | 4689  | CN      |
| 4  | 微博          | http://weibo.com/         | 20    | CN      |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
| 7  | stackoverflow | http://stackoverflow.com/ |   0 | IND     |
+----+---------------+---------------------------+-------+---------+

下面是 "apps" APP 的数据:

mysql> SELECT * FROM apps;
+----+------------+-------------------------+---------+
| id | app_name   | url                     | country |
+----+------------+-------------------------+---------+
|  1 | QQ APP     | http://im.qq.com/       | CN      |
|  2 | 微博 APP | http://weibo.com/       | CN      |
|  3 | 淘宝 APP | https://www.taobao.com/ | CN      |
+----+------------+-------------------------+---------+
3 rows in set (0.00 sec)

执行union:

SELECT country FROM Websites
UNION
SELECT country FROM apps
ORDER BY country;

image

执行union all:

SELECT country FROM Websites
UNION ALL
SELECT country FROM apps
ORDER BY country;

image

表操作相关

创建表

暂无

复制表

复制表结构及其数据

create table table_name_new as select * from table_name_old

只复制表结构

create table table_name_new as select * from table_name_old where 1=2;
-- 或者:
create table table_name_new like table_name_old

条件

start with connect by,查询树结构
select … from tablename
start with 条件1
connect by 条件2
where 条件3;
--例如:
select * from table
start with org_id = ‘HBHqfWGWPy’
connect by prior org_id = parent_id;

一个表中存在两个字段:org_id,parent_id,那么通过表示每一条记录的parent是谁,就可以形成一个树状结构,用上述语法的查询可以取得这棵树的所有记录,其中:

  • 条件1 是根结点的限定语句,当然可以放宽限定条件,以取得多个根结点,实际就是多棵树。
  • 条件2 是连接条件,其中用PRIOR表示上一条记录,比如 CONNECT BY PRIOR org_id = parent_id;就是说上一条记录的org_id 是本条记录的parent_id,即本记录的父亲是上一条记录。
  • 条件3 是过滤条件,用于对返回的所有记录进行过滤
CASE WHEN THEN ELSE END,条件语句,支持嵌套

嵌套方式

select case
		 when (1 = 1) then
			  case when(2=3) then
					   'A'
				  else  'K'
				  end
		 else
		  'b'
	   end
  from dual;
posted @ 2023-09-21 15:03  Fusio  阅读(15)  评论(0编辑  收藏  举报