Php MYSQL如何在3个表之间进行选择
我有三张桌子 表“员额”Php MYSQL如何在3个表之间进行选择,php,mysql,Php,Mysql,我有三张桌子 表“员额” post_id post_content post_topic 1 "Test" 1 2 "How are you" 1 3 "Hey" 1 4 "What" 2 表“主题” topic_id topic_name topic_forum 1 "Test 1" 1 2
post_id post_content post_topic
1 "Test" 1
2 "How are you" 1
3 "Hey" 1
4 "What" 2
表“主题”
topic_id topic_name topic_forum
1 "Test 1" 1
2 "Test 2" 2
表“论坛”
forum_id forum_name
1 "Food"
2 "Game"
例如,我想从帖子中选择所有记录,其中主题论坛为1所以在这种情况下,我应该使用JOIN?
有人能告诉我怎么做吗?谢谢。您需要使用连接查询,类似于下面的代码
选择p.*、t.*、f.forum\u id
从邮政p
在p.post\u topic=t.topic\u id上左连接主题t
在t.topic\u forum=f.forum\u id上左键加入论坛f
其中t.topic_forum=1
这将列出topic_forum为1的所有帖子
select * from posts p
left join topics t
on t.topic_id = p.post_topic
where t.topic_forum = 1
列出所有论坛名称为“食物”的帖子
select * from posts p
left join topics t
on t.topic_id = p.post_topic
left join forums f
on f.forum_id = t.topic_forum
where f.forum_name = 'Food'
这将从论坛名称为“食物”的帖子中选择所有记录
从帖子p、主题T、论坛F中选择p.*,其中F.forum\u id=T.topic\u forum和T.topic\u id=p.post\u topic和F.forum\u id=1'您所说的“post\u topic->topic\u forum=1”是什么意思?输出结果应该可以,但不需要左二加入到
论坛表。