mysql的使用16

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| MonthlyOrders  |
| Nums           |
| a              |
| animals        |
| charTest       |
| customers      |
| dept_manager   |
| emp            |
| employees      |
| g              |
| mintable       |
| new_emp        |
| orders         |
| sales          |
| sessions       |
| t              |
| test01         |
| timetest       |
| tt             |
| ttt            |
| updatetime     |
| x              |
| y              |
| yeartest       |
| z              |
+----------------+
25 rows in set (0.00 sec)

mysql> select * from x union select * from y;
+------+
| a    |
+------+
| a    |
| b    |
| c    |
| w    |
| q    |
+------+
5 rows in set (0.01 sec)

mysql> select * from x;
+------+
| a    |
+------+
| a    |
| b    |
| c    |
+------+
3 rows in set (0.00 sec)

mysql> select * from y;
+------+
| a    |
+------+
| a    |
| b    |
| w    |
| q    |
+------+
4 rows in set (0.00 sec)

mysql> #由此可以看出数据重复的部分去除了
mysql>
mysql> #mysql数据库对union distinct的实现方式如下
mysql> #1 创建一张临时表,即虚拟表
mysql> #2 对这张临时表的列添加唯一索引
mysql> #3 将输入的数据插入临时表
mysql> #4 返回虚拟表
mysql>
mysql> select a from x union select a from y;
+------+
| a    |
+------+
| a    |
| b    |
| c    |
| w    |
| q    |
+------+
5 rows in set (0.00 sec)

mysql> #现在我们 将重复选项剔除
mysql> select * from x union all
    -> select * from y;
+------+
| a    |
+------+
| a    |
| b    |
| c    |
| a    |
| b    |
| w    |
| q    |
+------+
7 rows in set (0.00 sec)

mysql> #except
mysql> #except集合操作允许用户找出位于第一个输入中但不属于第二个输入中的行数据
mysql> #except也可以分为except distinct 和 except all
mysql>
mysql> #以上也可使用那个left join 或者not exists
mysql>
mysql> select x.a from x left join y
    -> on x.a = y.a
    -> where y.a is null;
+------+
| a    |
+------+
| c    |
+------+
1 row in set (0.00 sec)

mysql> #x表中c是y表中没有的
mysql>
mysql> select a from x where not exists(select * from y where x.a=y.a);
+------+
| a    |
+------+
| c    |
+------+
1 row in set (0.00 sec)

mysql> create table xx(a char(1), b char(1))engine=innodb;
Query OK, 0 rows affected (0.10 sec)

mysql> create table yy(a char(1), b char(1))engine=innodb;
Query OK, 0 rows affected (0.09 sec)

mysql> insert into xx select 'a','b';
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into xx select b',NULL;
    '> insert into xx select 'c','d';
    '> insert into xx select 'c','d';
    '> insert into xx select 'c','d';
    '> insert into xx select 'c','c';
    '> insert into xx select 'e','f';
    '>
    '>
    '> insert into yy select 'a','b';
    '> insert into yy select 'b',NULL;
    '> insert into yy select 'c','d';
    '> insert into yy select 'c','c';
    '> ;
    '> '
    -> ';
    '> exit
    '> ;
    '> exit
    '> exit
    '> Ctrl-C -- exit!
Aborted
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值