推荐学习书目
› 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 · Dec 4, 2014 · 5638 views
    This topic created in 4357 days ago, the information mentioned may be changed or developed.
    在postgresql里,A用户有好友C和D,A想获取除C和D之外的其他用户,但是A的好友可能有多个,E、F、G可能都是A的好友。该如何做呢?
    13 replies  •  2014-12-06 21:55:09 +08:00
    ivanlw
    ivanlw
    Dec 4, 2014
    1
    能post一下表的结构会更好一些……
    O14
    O14
    Dec 4, 2014 via Android
    2
    可能是 SELECT * FROM users,friends WHERE friends.user_id=users.id and friends.id!=C and friends.id!=D

    请贴出表之间的关系
    p8p8
    p8p8
    OP
    Dec 4, 2014
    3
    我大概的说一下,有两个表,T1和T2,我从T2里去获取用户数据,但是这些用户数据,要在T1里,没有的。
    datou552211
    datou552211
    Dec 4, 2014
    4
    难道你有职能相同的两个表?
    p8p8
    p8p8
    OP
    Dec 4, 2014
    5
    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
    O14
    Dec 4, 2014 via Android
    6
    @p8p8 maybe 在FROM users 和NOT IN之间加个WHERE users.id

    mid和eid表示的一个属性吗?
    Narcissu5
    Narcissu5
    Dec 4, 2014
    7
    left join + is null
    tottichenzp
    tottichenzp
    Dec 4, 2014
    8
    not exists 或者 not in
    前者适用大表驱动小表,后者适用小表驱动大表
    1314258
    1314258
    Dec 4, 2014 via iPhone
    9
    可以用python的集合。
    MadbookPro
    MadbookPro
    Dec 4, 2014
    10
    这种事让Neo4j做会好很多吧?
    ispinfx
    ispinfx
    Dec 4, 2014
    11
    好像前10楼都把标题无视了……果然一谈技术就废寝忘餐……
    yueyoum
    yueyoum
    Dec 4, 2014
    12
    刚好最近在学习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
    p8p8
    OP
    Dec 6, 2014
    13
    已经搞定了,用 not in实现的。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1580 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 60ms · UTC 08:38 · PVG 16:38 · LAX 00:38 · JFK 03:38
    ♥ Do have faith in what you're doing.