如何根据条件从多个表中 select
How to select from multiple tables based on condition
我有两个table:
post table:
+-----+------------+--------------+
| id | title | details |
+-----+------------+--------------+
| 185 | some title | some details |
| 186 | some title | some details |
+-----+------------+--------------+
post 类别:
+----+------------+---------+
| id | category | post_id |
+----+------------+---------+
| 1 | some title | 185 |
| 2 | some title | 186 |
+----+------------+---------+
当用户点击类别时,我想根据类别 selected.
从 post table
中获取所有 posts
我可以 select category
和 post_id
像这样:
List<Map> postCategories2 = Base.findAll("select category, post_id from posts_categories where category = ?", request.queryParams("category_name"));
但我想要的是使用单个查询和 select id, title, details
来自 post table
使用 category and post_id
来自 table 2 即 category table
我拥有的所有信息都是类别名称,即 request.queryParams("category_name")
注:id and post_id
有主键-外键关系
我认为你必须为此使用连接
用这个查询替换你的查询
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
希望这对您有所帮助
我有两个table:
post table:
+-----+------------+--------------+
| id | title | details |
+-----+------------+--------------+
| 185 | some title | some details |
| 186 | some title | some details |
+-----+------------+--------------+
post 类别:
+----+------------+---------+
| id | category | post_id |
+----+------------+---------+
| 1 | some title | 185 |
| 2 | some title | 186 |
+----+------------+---------+
当用户点击类别时,我想根据类别 selected.
从post table
中获取所有 posts
我可以 select category
和 post_id
像这样:
List<Map> postCategories2 = Base.findAll("select category, post_id from posts_categories where category = ?", request.queryParams("category_name"));
但我想要的是使用单个查询和 select id, title, details
来自 post table
使用 category and post_id
来自 table 2 即 category table
我拥有的所有信息都是类别名称,即 request.queryParams("category_name")
注:id and post_id
有主键-外键关系
我认为你必须为此使用连接 用这个查询替换你的查询
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
希望这对您有所帮助