V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2016-12-05 18:57:51 +08:00 · 1706 次点击
    这是一个创建于 2705 天前的主题,其中的信息可能已经有所发展或是发生改变。

    数据表结构(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 次?)该怎么查询呢?

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   752 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 21:47 · PVG 05:47 · LAX 14:47 · JFK 17:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.