您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

mysql数据库常用的查询语句(多表查询)

2024/5/3 5:13:00发布31次查看
例如: 原表country mysql select * from country; ------------------------------------------ | id | name | population | area | language | ------------------------------------------ |1 | mysql | 13 | 960| chinese | |2 | american | 4 |60 | eng
例如: 
原表country
mysql> select * from country;
+----+----------+------------+------+----------+
| id | name     | population | area | language |
+----+----------+------------+------+----------+
| 1 | mysql    | 13         | 960 | chinese  |
| 2 | american | 4          |60   | english  |
| 3 | japan    | 89        | 30   | jpanese  |
| 4 | england  | 2         | 300  | english  |
+----+----------+------------+------+----------
原表library
mysql> select * from library;
+----+---------------+--------+-------+
| id | name          | author | price |
+----+---------------+--------+-------+
| 1 | java范例大全  | 张帆   | 99    |
| 2 | mysql         | 潘凯华 | 50    |
| 3 | sqlserver2005 | 刘智勇 | 80    |
| 4 | mysql         | 李慧  | 50    |
+----+---------------+--------+-------+
    mysql> select area,author from country,library where country.name=library.name;
+------+--------+
| area | author |
+------+--------+
| 960  | 潘凯华 |
| 960  | 李慧   |
+------+--------+
2 左外连接mysql> select language,area,author from country left join library on country.name=library.name; //返回的结果除内连接的数据外,还包括左表中不符合条件数据
+----------+------+--------+
| language | area | author |
+----------+------+--------+
| chinese  | 960 | 潘凯华 |
| chinese  | 960 | 李慧   |
| english  | 60  | null   |
| jpanese  | 30  | null   |
| english  | 300 | null   |
+----------+------+--------+   
3 右外连接
 mysql> select language,area,author from country right joinlibrary on country.name=library.name; // //返回的结果除内连接的数据外,还包括右表中不符合条件数据
+----------+------+--------+
| language | area | author |
+----------+------+--------+
| null     | null | 张帆   |
| chinese  | 960 | 潘凯华 |
| null     | null | 刘智勇 |
| chinese  | 960 | 李慧   |
+----------+------+--------+
4 复合条件连接查询
mysql>select population,area,author,price from country,library where country.name=library.name and price>30;
5 子查询
 1 带in关键字的子查询       mysql> select * from country wherename in(select name from library);
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product