V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · 2276 views
    This topic created in 2516 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
        1
    VeryZero  
       Jun 18, 2019   ❤️ 2
    完全不知道在说啥。。
    iblislsy
        2
    iblislsy  
       Jun 18, 2019
    完全不知道在说啥。。
    AngryPanda
        3
    AngryPanda  
       Jun 18, 2019
    语文不及格。
    KannaMakino
        4
    KannaMakino  
       Jun 18, 2019 via iPhone
    ???
    ashlord
        5
    ashlord  
       Jun 18, 2019
    额所以问题呢?
    sharpless
        6
    sharpless  
       Jun 18, 2019 via Android
    表达的很不准确
    akira
        7
    akira  
       Jun 18, 2019
    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
        8
    lyy16384  
       Jun 18, 2019
    说实话你的附言依然看不懂
    1010543618
        9
    1010543618  
       Jun 18, 2019
    感觉可以这样 xxx 代表一些 procedure

    select pid from (select xxx(type) as type_str, pid group by pid) where xxx(type_str)
    1010543618
        10
    1010543618  
       Jun 18, 2019
    貌似是要根据某个 pid 是否只包含某个 type,找出符合的 pid
    RRRoger
        11
    RRRoger  
       Jun 18, 2019
    SELECT * FROM table1 a
    WHERE NOT EXISTS
    (SELECT 1 FROM table1 b
    WHERE a.pid=b.pid
    AND a.type <b.type)
    maierhuang
        12
    maierhuang  
       Jun 18, 2019
    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   ·   Solana   ·   5533 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 117ms · UTC 07:01 · PVG 15:01 · LAX 00:01 · JFK 03:01
    ♥ Do have faith in what you're doing.