|
- [oracle@redflag11012501 home]$ mysql -uroot
- mysql: Unknown OS character set 'GB18030'.
- mysql: Switching to the default character set 'latin1'.
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 1
- Server version: 5.5.13-log MySQL Community Server (GPL)
- Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
- Oracle is a registered trademark of Oracle Corporation and/or its
- affiliates. Other names may be trademarks of their respective
- owners.
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- mysql> create table ou (dt date, uid int);
- ERROR 1046 (3D000): No database selected
- mysql> use test
- Database changed
- mysql> create table ou (dt date, uid int);
- Query OK, 0 rows affected (0.02 sec)
- mysql> create table nu (dt date, uid int);
- Query OK, 0 rows affected (0.01 sec)
- mysql> insert into ou values(date' 2012-11-1
- '> ',1);
- Query OK, 1 row affected (0.01 sec)
- mysql> insert into ou values(date' 2012-11-2
- ',3);
- Query OK, 1 row affected (0.01 sec)
- mysql> insert into ou values(date' 2012-11-5
- ',8);
- Query OK, 1 row affected (0.00 sec)
- mysql> insert into nu values(date' 2012-11-1
- ',5);
- Query OK, 1 row affected (0.00 sec)
- mysql> insert into nu values(date' 2012-11-2
- ',3);
- Query OK, 1 row affected (0.00 sec)
- mysql> select * from ou;
- +------------+------+
- | dt | uid |
- +------------+------+
- | 2012-11-01 | 1 |
- | 2012-11-02 | 3 |
- | 2012-11-05 | 8 |
- +------------+------+
- 3 rows in set (0.02 sec)
- mysql> select * from nu;
- +------------+------+
- | dt | uid |
- +------------+------+
- | 2012-11-01 | 5 |
- | 2012-11-02 | 3 |
- +------------+------+
- 2 rows in set (0.00 sec)
- mysql> select o.dt,ou.uid,nu.uid from(select dt from ou union select dt from nu)o left join ou using(dt) left join nu using(dt);
- +------------+------+------+
- | dt | uid | uid |
- +------------+------+------+
- | 2012-11-01 | 1 | 5 |
- | 2012-11-02 | 3 | 3 |
- | 2012-11-05 | 8 | NULL |
- +------------+------+------+
- 3 rows in set (0.00 sec)
- mysql> select o.dt,ifnull(ou.uid,0)newu,ifnull(nu.uid,0)oldu from(select dt from ou union select dt from nu)o left join ou using(dt) left join nu using(dt);
- +------------+------+------+
- | dt | newu | oldu |
- +------------+------+------+
- | 2012-11-01 | 1 | 5 |
- | 2012-11-02 | 3 | 3 |
- | 2012-11-05 | 8 | 0 |
- +------------+------+------+
- 3 rows in set (0.01 sec)
- mysql>
复制代码 |
|