MySql中左连接

mysql> use mydatabase;
Database changed

mysql> show tables;
+----------------------------+
| Tables_in_mydatabase       |
+----------------------------+
0 rows in set (0.00 sec)

mysql> create table outStore(
    -> outID VARCHAR(20) not null PRIMARY key,
    -> spID VARCHAR(20) not null,
    -> spName VARCHAR(20) not null,
    -> ckID VARCHAR(20) not null,
    -> ckQty VARCHAR(20) not null,
    -> glyID VARCHAR(20) not null,
    -> remark VARCHAR(20) not null
    -> );
Query OK, 0 rows affected (0.35 sec)

mysql> show tables;
+----------------------------+
| Tables_in_mydatabase       |
+----------------------------+
| outstore                   |
+----------------------------+
1 rows in set (0.00 sec)

mysql> create table Manager(
    -> spID VARCHAR(20) not null PRIMARY KEY,
    -> spName VARCHAR(20) not null
    -> )
    -> ;
Query OK, 0 rows affected (0.19 sec)

mysql> show tables;
+----------------------------+
| Tables_in_mydatabase       |
+----------------------------+
| manager                    |
| outstore                   |
+----------------------------+
2 rows in set (0.00 sec)

//insert into data
mysql> insert into outstore values('01','02','yinpan','ckId','ckqty','glyID','bucuo');
Query OK, 1 row affected (0.04 sec)

mysql> select * from outstore;
+-------+------+--------+------+-------+-------+--------+
| outID | spID | spName | ckID | ckQty | glyID | remark |
+-------+------+--------+------+-------+-------+--------+
| 01    | 02   | yinpan | ckId | ckqty | glyID | bucuo  |
+-------+------+--------+------+-------+-------+--------+
1 row in set (0.00 sec)

mysql> insert into manager values("01","mainframe"),
    -> ("02","hardDisk"),
    -> ("03","mouse"),
    -> ("04","keyboard"),
    -> ("05","screen"),
    -> ("06","mouse pad");
Query OK, 6 rows affected (0.04 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> select * from manager;
+------+-----------+
| spID | spName    |
+------+-----------+
| 01   | mainframe |
| 02   | hardDisk  |
| 03   | mouse     |
| 04   | keyboard  |
| 05   | screen    |
| 06   | mouse pad |
+------+-----------+
6 rows in set (0.00 sec)

现在开始使用左连接语句,进行查询:

select a.outID as 出库单号,
a.spID as 商品号,
a.spName as 商品名称,
a.ckID as 仓库号,
a.ckQty as 数量,
a.glyID as 管理员号,
a.Remark as 备注
from outStore a
left join 
(select b.spID from Manager b ) temp  on a.spID = temp.spID

执行结果如下:

+--------------+-----------+--------------+-----------+--------+--------------+--------+
| 出库单号      | 商品号     | 商品名称       | 仓库号     |数量    | 管理员号      | 备注   |
+--------------+-----------+--------------+-----------+--------+--------------+--------+
| 01           | 02        | yinpan       | ckId      | ckqty  | glyID        | bucuo  |
+--------------+-----------+--------------+-----------+--------+--------------+--------+
1 row in set (0.00 sec)

猜你喜欢

转载自blog.csdn.net/liu16659/article/details/80247238