Postgresql 的一个问题。

2014-12-08 05:00:01 +08:00
 p8p8
有一张users表结构(大致如下):

uid | avatar_url | nickname


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

uid | topic_name | topic_content

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

select * from topic where uid = 1;


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

卡在这里了,请教大家!

谢谢
2979 次点击
所在节点    Python
4 条回复
ivanlw
2014-12-08 05:50:46 +08:00
select avatar_url, nickname, topic_name, topic_content
from users, topic
where users.uid = topic.uid and
users.uid = 1
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
2014-12-08 09:43:11 +08:00
买本sql的书看一下吧,推荐head first sql
hsu
2014-12-08 10:16:40 +08:00
有两个小建议
1.不到万不得已,不要用select *
2.可以多看看SQL相关的基础知识

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/152252

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX