如何在mysql中组合3个表

如何在mysql中组合3个表,mysql,sql,mysql-workbench,Mysql,Sql,Mysql Workbench,我需要一些帮助 我有3张桌子,看起来像: user_base_info +-------+-----------+------------------+------------------+------------------+------------------+ |user_id| real_name | live_province | live_city | live_area_big | live_area_small | +-------+---------

我需要一些帮助

我有3张桌子,看起来像:

user_base_info
+-------+-----------+------------------+------------------+------------------+------------------+
|user_id| real_name |    live_province |   live_city      | live_area_big    | live_area_small  |
+-------+-----------+------------------+------------------+------------------+------------------+
|    1  |  John     |      15000000    |    15300000      |    15310000      |    15310003      |
|    2  |  Susan    |      42000000    |    42140000      |    42140400      |    42140401      |
|    3  |  Andy     |      12000000    |    58300000      |    58302000      |    58302004      |
|    4  |  Knoxvile |      12000000    |    12100000      |    12110000      |    12110002      |
|    5  |  Abraham  |      13000000    |    50200000      |    50200000      |    11115007      |
|  ...  |  ........ |      ...         |    ...           |    ...           |    ...           |
|331508 |  Donald   |      41000000    |    41010000      |    41011200      |    41011202      |
+-------+-----------+------------------+------------------+------------------+------------------+

borrow_progress
+--------+-----------+------------+------------+-------------+
|user_id | borrow_id |  state     |  remark    | create_time |
+--------+-----------+------------+------------+-------------+
|  170   |  236      |  10        |  waiting   |  24/04/17   |   
|  170   |  236      |  22        |  proceed   |  02/02/17   |     
|  170   |  236      |  26        |  success   |  25/04/17   |    
|  170   |  236      |  30        |  sent      |  05/11/17   |   
|  172   |  237      |  40        |  completed |  03/07/17   |   
|  ...   |  ...      |  ...       |  ...       |    ...      |   
| 353252 | 24112     |  90        |  failed    |  30/01/17   |   
+--------+-----------+------------+------------+-------------+

area
+------------+---------------------+
|code        |      name           |
+------------+---------------------+
| 11000000   | Jawa Tengah         |   
| 12000000   | Jawa Barat          |     
| 13000000   | Jawa Timur          |    
| 14000000   | Sumatera Utara      |   
| 15000000   | DKI Jakarta         |   
|  ...       |  ...                |   
| 41000000   | Di Yogyakarta       |  
| 42140000   | Kota Singkawang     | 
| 58300000   | Kota Depok          | 
| 12100000   | Bandung             | 
| 50200000   | Kabupaten Sukoharjo | 
| 41010000   | Kabupaten Sleman    | 
| 15310000   | Cilandak            | 
| 42140400   | Singkawang Barat    | 
| 41011200   | Mlati               | 
| 12110000   | Arjasari            | 
| 15310003   | Gandaria Selatan    | 
| 41011202   | Sinduadi            |   
+------------+---------------------+
我想在表user\U base\U info中选择状态为90的用户在表借用\U进度中,并在表区域中用名称更改live\u省、live\u市、live\u区\u大、live\u区\u小代码

我希望结果如下所示:

+-------+-----------+------------------+------------------+------------------+------------------+
|user_id| real_name |    live_province |   live_city      | live_area_big    | live_area_small  |
+-------+-----------+------------------+------------------+------------------+------------------+
|331508 |  Donald   |   Di Yogyakarta  | Kabupaten Sleman |    Mlati         |    Sinduadi      |
+-------+-----------+------------------+------------------+------------------+------------------+
我试过了

SELECT 
    *
FROM
    borrow_progress a
        INNER JOIN
    user_base_info b ON a.user_id = b.user_id
        INNER JOIN
    area c ON c.code = b.live_city
WHERE
    state = 90
但这不是我想要的。

试试这个:

SELECT 
    b.user_id
    , b.real_name
    , (select c1.name from area c1 where code = a.live_province) "live_province"
    , live_city      
    , (select c2.name from area c2 where code = a.live_area_big) "live_area_big"
    , (select c3.name from area c3 where code = a.live_area_big) "live_area_small"  
FROM borrow_progress a 
INNER JOIN user_base_info b ON a.user_id = b.user_id
INNER JOIN area c ON c.code = b.live_city
WHERE state = 90
试试这个:

SELECT 
    b.user_id
    , b.real_name
    , (select c1.name from area c1 where code = a.live_province) "live_province"
    , live_city      
    , (select c2.name from area c2 where code = a.live_area_big) "live_area_big"
    , (select c3.name from area c3 where code = a.live_area_big) "live_area_small"  
FROM borrow_progress a 
INNER JOIN user_base_info b ON a.user_id = b.user_id
INNER JOIN area c ON c.code = b.live_city
WHERE state = 90

必须将表
区域的4个副本连接到其他2个表
area
的每个副本都将返回以下四列中每一列的名称:
live\u province
live\u city
live\u area\u big
live\u area\u small

select u.user_id, u.real_name,
  a1.name live_province,
  a2.name live_city,
  a3.name live_area_big,
  a4.name live_area_small
from user_base_info u
inner join borrow_progress b on b.user_id= u.user_id
inner join area a1 on a1.code = u.live_province
inner join area a2 on a2.code = u.live_city
inner join area a3 on a3.code = u.live_area_big
inner join area a4 on a4.code = u.live_area_small
where b.state = 90
如果表
user\u base\u info
中的
live\u province
live\u city
live\u area\u big
live\u area\u small
user\u base\u info
null
,则使用
left
联接,而不是
inner
联接。
请参阅。
结果:


必须将表
区域的4个副本连接到其他2个表
area
的每个副本都将返回以下四列中每一列的名称:
live\u province
live\u city
live\u area\u big
live\u area\u small

select u.user_id, u.real_name,
  a1.name live_province,
  a2.name live_city,
  a3.name live_area_big,
  a4.name live_area_small
from user_base_info u
inner join borrow_progress b on b.user_id= u.user_id
inner join area a1 on a1.code = u.live_province
inner join area a2 on a2.code = u.live_city
inner join area a3 on a3.code = u.live_area_big
inner join area a4 on a4.code = u.live_area_small
where b.state = 90
如果表
user\u base\u info
中的
live\u province
live\u city
live\u area\u big
live\u area\u small
user\u base\u info
null
,则使用
left
联接,而不是
inner
联接。
请参阅。
结果:


当你说“不是我想要的”时,有什么特别的问题吗?它没有正确的列选择?它需要对区域表进行更多的联接才能查找其他活动代码?还有别的吗?@Rup我是个新手,所以我在这里询问是否有人可以修复我的问题query@Rup1.我是一个新手,所以这就是为什么我在这里问,如果有人可以修复我的查询。2.当我第一次加入时,我计算所有的行数=12173。第二次连接后,其为=12166。到底发生了什么?我们没有你的数据,所以我们无法确定你为什么有那么多行,但过滤数字的是“where”子句-你从
select*from borrow\u progress where state=90
?-在这之后,您希望行数保持不变,前提是在user_base_info中每个user_id正好有一行,在区域中每个代码正好有一行。如果联接表中每个ID有多行,则该数目将增加。如果缺少行,则该数字将下降。@EBUZ168该数字已删除,因为并非一个表中的所有用户id在另一个表中都有匹配的记录,并且与第三个表中的记录相同。您已使用特定条件联接了这三个表。如果此条件不成立,则不会显示行。希望这个解释能帮助你更好地理解。干杯当你说“不是我想要的”时,有什么特别的问题吗?它没有正确的列选择?它需要对区域表进行更多的联接才能查找其他活动代码?还有别的吗?@Rup我是个新手,所以我在这里询问是否有人可以修复我的问题query@Rup1.我是一个新手,所以这就是为什么我在这里问,如果有人可以修复我的查询。2.当我第一次加入时,我计算所有的行数=12173。第二次连接后,其为=12166。到底发生了什么?我们没有你的数据,所以我们无法确定你为什么有那么多行,但过滤数字的是“where”子句-你从
select*from borrow\u progress where state=90
?-在这之后,您希望行数保持不变,前提是在user_base_info中每个user_id正好有一行,在区域中每个代码正好有一行。如果联接表中每个ID有多行,则该数目将增加。如果缺少行,则该数字将下降。@EBUZ168该数字已删除,因为并非一个表中的所有用户id在另一个表中都有匹配的记录,并且与第三个表中的记录相同。您已使用特定条件联接了这三个表。如果此条件不成立,则不会显示行。希望这个解释能帮助你更好地理解。干杯谢谢你,先生。这超出了我的能力。我在这里学到了很多,谢谢你,先生。这超出了我的能力。我在这里学到了很多