sql & sqlalchemy join多个表

# 连接两个数据表的用法:
     FROM Member INNER JOIN MemberSort ON Member.MemberSort=MemberSort.MemberSort

# 语法格式可以概括为:
     FROM 表1 INNER JOIN 表2 ON 表1.字段号=表2.字段号
   
# 连接三个数据表的用法:
     FROM (Member INNER JOIN MemberSort ON Member.MemberSort=MemberSort.MemberSort) INNER JOIN MemberLevel ON Member.MemberLevel=MemberLevel.MemberLevel

# 语法格式可以概括为:
     FROM (表1 INNER JOIN 表2 ON 表1.字段号=表2.字段号) INNER JOIN 表3 ON 表1.字段号=表3.字段号
   
# 连接四个数据表的用法:
     FROM ((Member INNER JOIN MemberSort ON Member.MemberSort=MemberSort.MemberSort) INNER JOIN MemberLevel ON Member.MemberLevel=MemberLevel.MemberLevel) INNER JOIN MemberIdentity ON Member.MemberIdentity=MemberIdentity.MemberIdentity

# 语法格式可以概括为:
     FROM ((表1 INNER JOIN 表2 ON 表1.字段号=表2.字段号) INNER JOIN 表3 ON 表1.字段号=表3.字段号) INNER JOIN 表4 ON Member.字段号=表4.字段号
   
# 连接五个数据表的用法:
     FROM (((Member INNER JOIN MemberSort ON Member.MemberSort=MemberSort.MemberSort) INNER JOIN MemberLevel ON Member.MemberLevel=MemberLevel.MemberLevel) INNER JOIN MemberIdentity ON Member.MemberIdentity=MemberIdentity.MemberIdentity) INNER JOIN Wedlock ON Member.Wedlock=Wedlock.Wedlock

# 语法格式可以概括为:
     FROM (((表1 INNER JOIN 表2 ON 表1.字段号=表2.字段号) INNER JOIN 表3 ON 表1.字段号=表3.字段号) INNER JOIN 表4 ON Member.字段号=表4.字段号) INNER JOIN 表5 ON Member.字段号=表5.字段号

example

select resource_idc.uuid, resource_idc.name, resource_cabinet.uuid, resource_cabinet.alias from resource_idc 
    INNER JOIN resource_cabinet ON resource_idc.uuid = resource_cabinet.idc_uuid
order by resource_idc.uuid, resource_cabinet.alias;
+--------------------------------------+--------------+--------------------------------------+---------+
| uuid                                 | name         | uuid                                 | alias   |
+--------------------------------------+--------------+--------------------------------------+---------+
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 66f03a52-7716-454c-b69b-77863b40f871 | wxxq-b2 |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | ea6c9cf9-aa67-429a-b934-c602c7ffcf47 | wxxq-b3 |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | fb6e260a-e4c0-43d4-b00a-f33d41fee0de | wxxq-b4 |
| ffdba1b7-6f88-4f78-a1de-b737e682642b | 北京铜牛     | 6b54116c-c6d6-4901-85af-c956d09cfbd5 | bjtn-a1 |
+--------------------------------------+--------------+--------------------------------------+---------+
select resource_idc.uuid, resource_idc.name, resource_cabinet.uuid, resource_cabinet.alias, resource_rack.uid, resource_rack.host_uuid 
from (resource_idc INNER JOIN resource_cabinet ON resource_idc.uuid = resource_cabinet.idc_uuid)
    INNER JOIN resource_rack ON resource_cabinet.uuid = resource_rack.cabinet_uuid
where resource_cabinet.uuid='2c71ecba-4b14-4b94-a623-1f6154b740aa'
order by resource_rack.uid;
+--------------------------------------+--------------+--------------------------------------+---------+------+-----------+
| uuid                                 | name         | uuid                                 | alias   | uid  | host_uuid |
+--------------------------------------+--------------+--------------------------------------+---------+------+-----------+
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |    1 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |    2 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |    3 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |    4 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |    5 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |    6 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |    7 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |    8 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |    9 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   10 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   11 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   12 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   13 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   14 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   15 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   16 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   17 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   18 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   19 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   20 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   21 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   22 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   23 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   24 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   25 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   26 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   27 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   28 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   29 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   30 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   31 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   32 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   33 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   34 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   35 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   36 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   37 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   38 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   39 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   40 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   41 | NULL      |
| eb4fb047-a4fd-44c2-9f10-15758187417a | 无锡新区     | 2c71ecba-4b14-4b94-a623-1f6154b740aa | wxxq-b1 |   42 | NULL      |
+--------------------------------------+--------------+--------------------------------------+---------+------+-----------+

http://stackoverflow.com/questions/27900018/flask-sqlalchemy-query-join-relational-tables
many2many query
http://stackoverflow.com/questions/25392770/query-many-to-many-in-sqlalchemy/

posted on 2015-10-20 12:42 北京涛子 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/liujitao79/p/4894456.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值