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
a476286557
V2EX  ›  Python

数据库查询数据问题

  •  
  •   a476286557 · 2019-06-18 17:04:33 +08:00 · 1728 次点击
    这是一个创建于 1764 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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,所以不取。

    第 1 条附言  ·  2019-06-18 19:09:35 +08:00

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

    12 条回复    2019-06-18 19:56:41 +08:00
    VeryZero
        1
    VeryZero  
       2019-06-18 17:49:27 +08:00   ❤️ 2
    完全不知道在说啥。。
    iblislsy
        2
    iblislsy  
       2019-06-18 18:30:50 +08:00
    完全不知道在说啥。。
    AngryPanda
        3
    AngryPanda  
       2019-06-18 18:32:32 +08:00
    语文不及格。
    KannaMakino
        4
    KannaMakino  
       2019-06-18 18:40:34 +08:00 via iPhone
    ???
    ashlord
        5
    ashlord  
       2019-06-18 18:42:22 +08:00
    额所以问题呢?
    sharpless
        6
    sharpless  
       2019-06-18 18:48:52 +08:00 via Android
    表达的很不准确
    akira
        7
    akira  
       2019-06-18 18:57:26 +08:00
    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  
       2019-06-18 19:18:00 +08:00
    说实话你的附言依然看不懂
    1010543618
        9
    1010543618  
       2019-06-18 19:36:57 +08:00
    感觉可以这样 xxx 代表一些 procedure

    select pid from (select xxx(type) as type_str, pid group by pid) where xxx(type_str)
    1010543618
        10
    1010543618  
       2019-06-18 19:38:52 +08:00
    貌似是要根据某个 pid 是否只包含某个 type,找出符合的 pid
    RRRoger
        11
    RRRoger  
       2019-06-18 19:51:17 +08:00
    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  
       2019-06-18 19:56:41 +08:00
    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));
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   948 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 20:54 · PVG 04:54 · LAX 13:54 · JFK 16:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.