• 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/

  • 相关阅读:
    mysql显示乱码
    aws常用命令
    Hive分析窗口函数(一) SUM,AVG,MIN,MAX
    Hive函数介绍
    Apache Drill 调研学习
    公有云与私有云对比分析报告
    python3 使用libvirt 相关安装
    libvirt虚拟库
    Reveal CocoaPods的使用
    AFNetworking 2.0 使用
  • 原文地址:https://www.cnblogs.com/liujitao79/p/4894456.html
Copyright © 2020-2023  润新知