-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"));

但我想要的是使用单个查询并从id, title, details表2 中选择,即post tablecategory and post_idcategory table

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

注意:id and post_id有主键-外键关系

4

1 回答 1

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

希望这可以帮助

于 2017-06-17T12:05:24.647 回答