Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅显示用户使用mySQL跟踪的用户的帖子_Mysql_Sql_Relational Database - Fatal编程技术网

仅显示用户使用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;

对不起,我忘了提一下,我试过了,但出现了语法错误