mysql連線查詢

2021-07-25 09:29:49 字數 1788 閱讀 8492

mysql> select * from name_address;

+----------+------+----+

| address | name | id |

+----------+------+----+

| 西北一路 | 張三 | 1 |

| 西北二路 | 李四 | 2 |

| 西北三路 | 王五 | 3 |

+----------+------+----+

3 rows in set

mysql> select * from name_age;

+-----+--------+----+

| age | name | id |

+-----+--------+----+

| 18 | 張三 | 1 |

| 20 | 王五 | 2 |

| 21 | 路人甲 | 3 |

+-----+--------+----+

3 rows in set

1、inner join

inner join與一般的連表查詢一致,即使用逗號隔開的查詢方式。

mysql> select a.`name`,a.age,b.address from name_age a inner join name_address b where(on) a.`name`=b.`name`;

+------+-----+----------+

| name | age | address |

+------+-----+----------+

| 張三 | 18 | 西北一路 |

| 王五 | 20 | 西北三路 |

+------+-----+----------+

2 rows in set

2、left join

以左邊的資料表為準

mysql> select a.`name`,a.age,b.address from name_age a left join name_address b on

a.`name`=b.`name`;

+--------+-----+----------+

| name | age | address |

+--------+-----+----------+

| 張三 | 18 | 西北一路 |

| 王五 | 20 | 西北三路 |

| 路人甲 | 21 | null |

+--------+-----+----------+

3 rows in set

3、right join

與left join相反,即以右邊的資料為準

mysql> select b.`name`,a.age,b.address from name_age a right join name_address b on a.`name`=b.`name`;

+------+------+----------+

| name | age | address |

+------+------+----------+

| 張三 | 18 | 西北一路 |

| 王五 | 20 | 西北三路 |

| 李四 | null | 西北二路 |

+------+------+----------+

3 rows in set

mysql連線查詢例項 MySQL連線查詢例項詳解

建立表suppliers create table suppliers s id int not null auto increment,s name char 50 not null,s city char 50 null,s zip char 10 null,s call char 50 not...

mysql連線查詢例項 MySQL連線查詢例項詳解

建立表suppliers create table suppliers s id int not null auto increment,s name char 50 not null,s city char 50 null,s zip char 10 null,s call char 50 not...

mysql連線查詢on MySQL連線查詢例項詳解

建立表suppliers create table suppliers s id int not null auto increment,s name char 50 not null,s city char 50 null,s zip char 10 null,s call char 50 not...