仅显示用户使用mySQL跟踪的用户的帖子
必要表格仅显示用户使用mySQL跟踪的用户的帖子,mysql,sql,relational-database,Mysql,Sql,Relational Database,必要表格 recipe_post +-----------+-----------+-------+ | recipe_id | posted_by | title | +-----------+-----------+-------+ friend_request (followers table) +----------+-----------+ | follower | following | +----------+-
recipe_post
+-----------+-----------+-------+
| recipe_id | posted_by | title |
+-----------+-----------+-------+
friend_request (followers table)
+----------+-----------+
| follower | following |
+----------+-----------+
此查询从recipe_配料表获取配料,并将其与users_配料表匹配,并返回所有状态为用户是否可以烹饪的帖子
SELECT u.uid, ri.recipe_id,
COUNT(ui.i_id) AS available_ingredients, -- Number of ingredients the user has that are required to cook this recipe
COUNT(ri.i_id) AS required_ingredients, -- Number of ingredients that are required to cook this recipe
CASE
WHEN COUNT(ui.i_id) = COUNT(ri.i_id) THEN 'can_cook'
WHEN COUNT(ui.i_id) > 0 THEN 'has_some_ingredients'
ELSE 'has_no_ingredients'
END AS state,
rp.recipe_id,rp.name,rp.description
FROM users u
CROSS JOIN recipe_ingredients ri
LEFT JOIN userIngredients ui ON(ri.i_id = ui.i_id AND u.uid = ui.uid)
INNER JOIN recipe_post rp ON rp.recipe_id = ri.recipe_id
WHERE u.uid = 1 --matching with user 1
GROUP BY u.uid, ri.recipe_id, rp.recipe_id
ORDER BY u.uid, ri.recipe_id;
我创建了另一个查询,它只返回他们关注的用户的帖子
SELECT * FROM recipe_post p
INNER JOIN friend_requests f ON (f.following = p.posted_by)
WHERE f.follower = 5;
我在将此查询添加到第一个查询时遇到问题,因此它只能显示他们关注的用户的帖子您可以将第二个查询作为子查询加入
SELECT u.uid
, ri.recipe_id
, COUNT(ui.i_id) AS available_ingredients -- Number of ingredients the user has that are required to cook this recipe
, COUNT(ri.i_id) AS required_ingredients -- Number of ingredients that are required to cook this recipe
, CASE WHEN COUNT(ui.i_id) = COUNT(ri.i_id) THEN 'can_cook'
WHEN COUNT(ui.i_id) > 0 THEN 'has_some_ingredients'
ELSE 'has_no_ingredients'
END AS state
, rp.recipe_id
, rp.name
, rp.description
FROM users u
CROSS JOIN recipe_ingredients ri
LEFT JOIN userIngredients ui ON(ri.i_id = ui.i_id AND u.uid = ui.uid)
INNER JOIN recipe_post rp ON rp.recipe_id = ri.recipe_id
INNER JOIN
(SELECT * FROM recipe_post p
INNER JOIN friend_requests f ON f.following = p.posted_by) as t1 on t1.recipe_id = = rp.recipe_id
WHERE u.uid = 1 --matching with user 1
GROUP BY u.uid, ri.recipe_id, rp.recipe_id
ORDER BY u.uid, ri.recipe_id;
您可以直接添加如何使用其他表进行连接:
SELECT u.uid, ri.recipe_id,
COUNT(ui.i_id) AS available_ingredients, -- Number of ingredients the user has that are required to cook this recipe
COUNT(ri.i_id) AS required_ingredients, -- Number of ingredients that are required to cook this recipe
CASE
WHEN COUNT(ui.i_id) = COUNT(ri.i_id) THEN 'can_cook'
WHEN COUNT(ui.i_id) > 0 THEN 'has_some_ingredients'
ELSE 'has_no_ingredients'
END AS state,
rp.recipe_id,rp.name,rp.description
FROM users u
CROSS JOIN recipe_ingredients ri
LEFT JOIN userIngredients ui ON ri.i_id = ui.i_id AND u.uid = ui.uid
INNER JOIN recipe_post rp ON rp.recipe_id = ri.recipe_id
INNER JOIN friend_requests f ON f.following = rp.posted_by
WHERE u.uid = 1 --matching with user 1
AND f.follower = 5
GROUP BY u.uid, ri.recipe_id, rp.recipe_id
ORDER BY u.uid, ri.recipe_id;
对不起,我忘了提一下,我试过了,但出现了语法错误