推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
rogwan
V2EX  ›  Python

SQLAlchemy 在同一个表中,求交集,该怎么查询呢?

  •  
  •   rogwan · Dec 5, 2016 · 2457 views
    This topic created in 3588 days ago, the information mentioned may be changed or developed.

    数据表结构(student_id / class_id / room_id 三个字段全是多对多关系,三个字段组成联合 primary key)如下:

    student_id - class_id - room_id
    student1 --- english --- 1
    student1 --- english --- 2
    student1 --- math --- 3
    student2 --- math --- 3
    student2 --- Chinese --- 2
    student3 --- math --- 3
    student4 --- english --- 1
    student4 --- english --- 3
    student5 --- english --- 1
    student5 --- english --- 2
    student5 --- english --- 6
    student5 --- math --- 3

    1 、求同时在上 english 和 math 课的学员名单;

    2 、拓展一下:学员人数再增加,课程科目再增加,教室数量再增加。求给定同时在上某几个科目的学员名单,要怎么查询呢?比如:求同时在上 english 、 math 、 history 三个科目的学员名单?求同时在上 N 个科目的学员名单?

    SQL 可以用 InterSect 来查询不同表的交集,但是同一个表的交集、且如果是查询 N 个科目,就是说别名的表的数量也是不确定的,要怎么查呢?

    用 SQLAlchemy 来处理,也会遇到同样的问题,用 inner join 来查询,遇到 N 个科目(就是要别名 N 次?)该怎么查询呢?

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2518 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 13:46 · PVG 21:46 · LAX 06:46 · JFK 09:46
    ♥ Do have faith in what you're doing.