MySQL SELECT 同时查询多个表

MySQL SELECT query from multiple tables at the same time

我的数据库中有这样的架构:

我想显示特定用户(用 unique_id 给出)添加到他的收藏夹中的所有食谱(具有特定字段),这些食谱在 table 收藏夹(2 个字段 - 用户 ID 和喜欢的食谱)。我不知道该怎么做。

f.e。如果用户喜欢 5 个食谱,则该信息包含在收藏夹 table(他的 id 和他喜欢的食谱 id)中。我要显示字段:

我试着用这个做一些查询:

SELECT recipe.`unique_id`, recipe.`title`, recipe.`img_tumbnail_link`, recipe.`add_date`, recipe.`kitchen_type`, recipe.`meal_type`, user.`name`, user.`surname`, COUNT(`like`.`recipe_unique_id_fk`) AS like_count 
FROM `recipe` 
JOIN `favourite` ON (recipe.`unique_id` = `favourite`.`recipe_unique_id_fk`) 
JOIN `like` ON (recipe.`unique_id` = `like`.`recipe_unique_id_fk`) 
JOIN `user` ON (recipe.`user_unique_id_fk` = user.`unique_id`)
WHERE favourite.`user_unique_id_fk` = '565096811c0b51.08471830' 
ORDER BY recipe.`add_date` DESC

来自 table 这样的:

通过此查询,我只收到 1 行而不是 3 行,我应该为 ID 为 565096811c0b51.08471830 的用户获取。有什么帮助吗?谢谢。

我添加了食谱 table 和结果:)

这是我的查询结果:

这是所有记录(无重复):http://postimg.org/image/ejjemnozb/

您正在使用与 like table 的连接,它 只有一行 这就是为什么只有一行回来。您可以使用 子查询 来计算喜欢。我在下面提到了正确的查询:

SELECT recipe.`unique_id`, recipe.`title`, recipe.`img_tumbnail_link`, recipe.`add_date`, recipe.`kitchen_type`, recipe.`meal_type`, user.`name`, user.`surname`, 
(SELECT count(*) from `like` WHERE recipe.`unique_id` = `like`.`recipe_unique_id_fk`) AS like_count
FROM `recipe` 
JOIN `favourite` ON (recipe.`unique_id` = `favourite`.`recipe_unique_id_fk`) 
JOIN `user` ON (recipe.`user_unique_id_fk` = user.`unique_id`)
WHERE favourite.`user_unique_id_fk` = '565096811c0b51.08471830' 
ORDER BY recipe.`add_date` DESC