推荐学习书目
› 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
a476286557
V2EX  ›  Python

数据库查询数据问题

  •  
  •   a476286557 · Jun 18, 2019 · 2879 views
    This topic created in 2701 days ago, the information mentioned may be changed or developed.
    id  pid  type
    1    1    2
    2    1    7
    3    1    8
    4    2    8
    5    3    7
    6    3    8
    

    表结构如上。
    pid 与 id 没有任何关系
    取出来这两种数据:
    1。pid 为 2 的,因为 type 只等于 8
    2。pid 为 3 的,因为 type 只有等与 7 与等于 8 的
    注:pid 为 1 的不需要,因为有一个 type=2,所以不取。

    Supplement 1  ·  Jun 18, 2019

    对不起大家了,没描述清楚。 就是要取type=8的或仅有type=8及type=7的pid。 上述例子中,符合条件的只有2,3

    12 replies  •  2019-06-18 19:56:41 +08:00
    VeryZero
    VeryZero
    Jun 18, 2019❤️2
    1
    完全不知道在说啥。。
    iblislsy
    iblislsy
    Jun 18, 2019
    2
    完全不知道在说啥。。
    AngryPanda
    AngryPanda
    Jun 18, 2019
    3
    语文不及格。
    KannaMakino
    KannaMakino
    Jun 18, 2019 via iPhone
    4
    ???
    ashlord
    ashlord
    Jun 18, 2019
    5
    额所以问题呢?
    sharpless
    sharpless
    Jun 18, 2019 via Android
    6
    表达的很不准确
    akira
    akira
    Jun 18, 2019
    7
    1。pid 为 2 的,因为 type 只等于 8
    // pid=2 和 type=8 有什么逻辑关系?

    2。pid 为 3 的,因为 type 只有等与 7 与等于 8 的
    // pid=3 和 type=7/8 又是什么逻辑关系

    注:pid 为 1 的不需要,因为有一个 type=2,所以不取
    // pid=1 和 type =2 又又是什么逻辑关系
    lyy16384
    lyy16384
    Jun 18, 2019
    8
    说实话你的附言依然看不懂
    1010543618
    1010543618
    Jun 18, 2019
    9
    感觉可以这样 xxx 代表一些 procedure

    select pid from (select xxx(type) as type_str, pid group by pid) where xxx(type_str)
    1010543618
    1010543618
    Jun 18, 2019
    10
    貌似是要根据某个 pid 是否只包含某个 type,找出符合的 pid
    RRRoger
    RRRoger
    Jun 18, 2019
    11
    SELECT * FROM table1 a
    WHERE NOT EXISTS
    (SELECT 1 FROM table1 b
    WHERE a.pid=b.pid
    AND a.type <b.type)
    maierhuang
    maierhuang
    Jun 18, 2019
    12
    select * from t1 where pid in (select distinct pid from t1 where type in (7,8) and pid not in (select distinct pid from t1 where type !=7 and type !=8));
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1139 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 23:12 · PVG 07:12 · LAX 15:12 · JFK 18:12
    ♥ Do have faith in what you're doing.