网络编程
位置:首页>> 网络编程>> 数据库>> 使用一条INSERT语句完成多表插入(4)

使用一条INSERT语句完成多表插入(4)

 来源:asp之家 发布时间:2010-03-18 11:08:00 

标签:INSERT语句,MySQL多表,MySQL技巧


5)为真实的反映“数据只插入一次”的目的,我们把条件颠倒后再插入一次。


sec@ora10g> delete from t1;
sec@ora10g> delete from t2;

sec@ora10g> insert first when x>=2 then into t1 when x>=5 then into t2 select * from t;

5 rows created.

sec@ora10g> select * from t1;

X Y
---------- ----------
2 b
3 c
4 d
5 e
6 f

sec@ora10g> select * from t2;

no rows selected

OK,目的达到,可见满足第二个条件的数据已经包含在第一个条件里,所以不会有数据插入到第二张表。

同样的插入条件,我们把“INSERT FIRST”换成“INSERT ALL”,对比一下结果。


sec@ora10g> delete from t1;

5 rows deleted.

sec@ora10g> delete from t2;

0 rows deleted.

sec@ora10g> insert all when x>=2 then into t1 when x>=5 then into t2 select * from t;

7 rows created.

sec@ora10g> select * from t1;

X Y
---------- ----------
2 b
3 c
4 d
5 e
6 f

sec@ora10g> select * from t2;

X Y
---------- ----------
5 e
6 f

是不是在豁然开朗的基础上又有一种锦上添花的感觉。That's it.

6.Oralce官方文档参考链接

http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_9014.htm#SQLRF01604

7.小结

这些小小小的高级SQL技巧在实际的应用中有很大用处。慢慢体会吧。


Good luck.

secooler
10.01.06

-- The End --

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com