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

Postgresql 的一个问题。

  •  
  •   p8p8 · Dec 8, 2014 · 4087 views
    This topic created in 4319 days ago, the information mentioned may be changed or developed.
    有一张users表结构(大致如下):

    uid | avatar_url | nickname


    还有一张topic表结构(大致如下):

    uid | topic_name | topic_content

    我想获取 uid = 1的用户的所有topic帖子,所以我写了这样的语句

    select * from topic where uid = 1;


    但是这个时候,我还需要得到用户的昵称和Avatar_url,所以我还需要根据uid去users表里拿一下,

    卡在这里了,请教大家!

    谢谢
    4 replies  •  2014-12-08 10:16:40 +08:00
    ivanlw
        1
    ivanlw  
       Dec 8, 2014 via iPhone
    select avatar_url, nickname, topic_name, topic_content
    from users, topic
    where users.uid = topic.uid and
    users.uid = 1
    raptor
        2
    raptor  
       Dec 8, 2014
    这跟postgresql没什么必然关系啊,就是个标准SQL语句的问题……

    select u.avatar_url, u.nickname, t.* from topic t inner join users u on u.uid=t.uid where t.uid=1
    F281M6Dh8DXpD1g2
        3
    F281M6Dh8DXpD1g2  
       Dec 8, 2014 via iPhone
    买本sql的书看一下吧,推荐head first sql
    hsu
        4
    hsu  
       Dec 8, 2014
    有两个小建议
    1.不到万不得已,不要用select *
    2.可以多看看SQL相关的基础知识
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2408 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 12:10 · PVG 20:10 · LAX 05:10 · JFK 08:10
    ♥ Do have faith in what you're doing.