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

没办法了,又一个通宵了。

  •  
  •   p8p8 · 2014-12-04 07:31:21 +08:00 · 4216 次点击
    这是一个创建于 3425 天前的主题,其中的信息可能已经有所发展或是发生改变。
    在postgresql里,A用户有好友C和D,A想获取除C和D之外的其他用户,但是A的好友可能有多个,E、F、G可能都是A的好友。该如何做呢?
    13 条回复    2014-12-06 21:55:09 +08:00
    ivanlw
        1
    ivanlw  
       2014-12-04 07:32:56 +08:00
    能post一下表的结构会更好一些……
    O14
        2
    O14  
       2014-12-04 07:47:03 +08:00 via Android
    可能是 SELECT * FROM users,friends WHERE friends.user_id=users.id and friends.id!=C and friends.id!=D

    请贴出表之间的关系
    p8p8
        3
    p8p8  
    OP
       2014-12-04 07:53:22 +08:00
    我大概的说一下,有两个表,T1和T2,我从T2里去获取用户数据,但是这些用户数据,要在T1里,没有的。
    datou552211
        4
    datou552211  
       2014-12-04 08:19:24 +08:00
    难道你有职能相同的两个表?
    p8p8
        5
    p8p8  
    OP
       2014-12-04 08:19:38 +08:00
    query_mid = 'SELECT mid, nickname, avatar_url FROM users NOT IN (SELECT eid FROM rosterusers WHERE username = %s AND subscription != "F" ' \
    'AND subscription != "T" AND subscription != "N");'

    我想表达的意思是,得到users表中,所有rosterusers表里username != xx和 subscription != "F" ' \
    'AND subscription != "T" AND subscription != "N"的数据。
    O14
        6
    O14  
       2014-12-04 08:25:01 +08:00 via Android
    @p8p8 maybe 在FROM users 和NOT IN之间加个WHERE users.id

    mid和eid表示的一个属性吗?
    Narcissu5
        7
    Narcissu5  
       2014-12-04 08:59:40 +08:00
    left join + is null
    tottichenzp
        8
    tottichenzp  
       2014-12-04 09:02:56 +08:00
    not exists 或者 not in
    前者适用大表驱动小表,后者适用小表驱动大表
    1314258
        9
    1314258  
       2014-12-04 09:14:25 +08:00 via iPhone
    可以用python的集合。
    MadbookPro
        10
    MadbookPro  
       2014-12-04 10:12:12 +08:00
    这种事让Neo4j做会好很多吧?
    ispinfx
        11
    ispinfx  
       2014-12-04 10:25:59 +08:00
    好像前10楼都把标题无视了……果然一谈技术就废寝忘餐……
    yueyoum
        12
    yueyoum  
       2014-12-04 17:01:07 +08:00
    刚好最近在学习postgresql,就来解答一下

    LZ这样的 没有表结构, 怎么回答?

    那我就来假设吧:


    如果是经典的关系表:
    那么你会有两个表, user 和 friends,

    user 记录用户, friends 记录好友关系


    mydb=# \d user
    Table "public.user"
    Column | Type | Modifiers
    --------+---------+-----------
    id | integer

    mydb=# \d friends
    Table "public.friends"
    Column | Type | Modifiers
    --------+---------+-----------
    u1 | integer |
    u2 | integer |

    mydb=# select * from user ;
    id
    ----
    1
    2
    3
    4
    5
    6
    (6 rows)

    mydb=# select * from friends ;
    u1 | u2
    ----+----
    1 | 3
    1 | 6
    (2 rows)


    假设 user.id = 1 的是A用户, 他有两个好友,id为3,6

    SQL:


    mydb=# select id from user, (select array_agg(u2) as friend from friends where u1 = 1) as f where not (id = any(f.friend));
    id
    ----
    1
    2
    4
    5
    (4 rows)



    如果是单向好友,那么完全可以不要friends表, 把好友用array类型存储起来


    mydb=# \d s_user
    Table "public.s_user"
    Column | Type | Modifiers
    ---------+-----------+-----------
    id | integer |
    friends | integer[] |



    mydb=# select * from s_user ;
    id | friends
    ----+---------
    2 |
    3 |
    4 |
    5 |
    6 |
    1 | {3,6}
    (6 rows)

    mydb=# select u1.id from s_user u1, s_user u2 where u2.id = 1 and not (u1.id = any(u2.friends));
    id
    ----
    2
    4
    5
    1
    (4 rows)
    p8p8
        13
    p8p8  
    OP
       2014-12-06 21:55:09 +08:00
    已经搞定了,用 not in实现的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2841 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 06:31 · PVG 14:31 · LAX 23:31 · JFK 02:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.