2017-06-17 110 views
-1

多个表中选择我有两个表:如何从基于条件

交表:

+-----+------------+--------------+ 
| id | title | details | 
+-----+------------+--------------+ 
| 185 | some title | some details | 
| 186 | some title | some details | 
+-----+------------+--------------+ 

岗位类别:

+----+------------+---------+ 
| id | category | post_id | 
+----+------------+---------+ 
| 1 | some title |  185 | 
| 2 | some title |  186 | 
+----+------------+---------+ 

当类别的用户点击,我想根据所选类别获取post table的所有帖子。

我能够选择categorypost_id这样的:

List<Map> postCategories2 = Base.findAll("select category, post_id from posts_categories where category = ?", request.queryParams("category_name")); 

但我要的是使用category and post_id从表中使用一个单一的查询,然后选择id, title, detailspost table 2即category table

所有我的信息是类别名称即request.queryParams("category_name")

注:id and post_id有主键,外键关系

+0

提示更换您的查询:'JOIN'表一起。 –

回答

1

我认为你必须使用该 联接与此查询

select title,details,category from post p inner join posts_categories c on 
p.id=c.post_id where category= ?//your category name at the question mark 

希望这有助于

+0

我在帖子表中没有'category'。在'posts_categories c'中'c'是什么意思? – kittu

+1

我实际上使用了连接。 category是该列的名称,而c只是posts_categories表的对象,而不是一次又一次地写入该表的名称。我实际上是从post_categories表中的帖子表和类别中提取标题和详细信息 –