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

Postgresql 的一个问题。

  •  
  •   p8p8 · 2014-12-08 05:00:01 +08:00 · 2966 次点击
    这是一个创建于 3429 天前的主题,其中的信息可能已经有所发展或是发生改变。
    有一张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 条回复    2014-12-08 10:16:40 +08:00
    ivanlw
        1
    ivanlw  
       2014-12-08 05:50:46 +08:00 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  
       2014-12-08 09:07:50 +08:00
    这跟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
    liprais
        3
    liprais  
       2014-12-08 09:43:11 +08:00 via iPhone
    买本sql的书看一下吧,推荐head first sql
    hsu
        4
    hsu  
       2014-12-08 10:16:40 +08:00
    有两个小建议
    1.不到万不得已,不要用select *
    2.可以多看看SQL相关的基础知识
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   864 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 21:14 · PVG 05:14 · LAX 14:14 · JFK 17:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.