MySQL - 从2个表中获取数据

时间:2014-06-06 09:35:15

标签: mysql

好吧,我在这里有两张表,gforum_Postgforum_PostView

我试图从表gforum_Post获取具有相同post_id的信息。

"SELECT post_subject, post_username FROM `gforum_Post` WHERE post_id = 341"

我已经让它显示我想要的东西,但它的硬编码,列中的数据会发生变化。这是我到目前为止所做的事情

SELECT post_id_fk, post_thread_views FROM `gforum_PostView` ORDER BY `gforum_PostView`.`post_thread_views` DESC

SELECT post_subject, post_username FROM `gforum_Post` WHERE post_id = 341

341是硬编码的值

2 个答案:

答案 0 :(得分:1)

您可以使用join来加入表格:

SELECT post_id_fk, post_thread_views, post_subject, post_username FROM `gforum_PostView` 
join `gforum_Post` on post_id =post_id_fk
ORDER BY `gforum_PostView`.`post_thread_views` DESC

答案 1 :(得分:1)

我认为您正在寻找的是JOIN查询:

SELECT GPV.post_id_fk, GPV.post_thread_views, GP.post_subject, GP.post_username 
FROM gforum_Post GP, gforum_PostView GPV 
WHERE GP.post_id = 341
ORDER BY GPV.post_thread_views DESC