如何INNER JOIN围绕一个表循环

时间:2013-12-03 05:30:43

标签: mysql cakephp inner-join

我有四张表如下:

CREATE TABLE IF NOT EXISTS `categories` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;

CREATE TABLE IF NOT EXISTS `categories_friends` (
  `category_id` int(10) unsigned NOT NULL,
  `friend_id` int(10) unsigned NOT NULL,
  UNIQUE KEY `category_id` (`friend_id`,`category_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `friends` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(10) unsigned NOT NULL,
  `friend_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_id` (`user_id`,`friend_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;

CREATE TABLE IF NOT EXISTS `ratings` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(10) unsigned NOT NULL,
  `category_id` int(10) unsigned NOT NULL,
  `title` varchar(255) NOT NULL,
  `description` text NOT NULL,
  `rating` tinyint(2) unsigned NOT NULL,
  `public` tinyint(1) NOT NULL DEFAULT '0',
  `created` datetime NOT NULL,
  PRIMARY KEY (`id`),
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;

我正在尝试对这些表执行以下查询:

SELECT *
FROM `favred`.`ratings` AS `Rating`
    INNER JOIN `favred`.`friends` AS `JFriend`
        ON (`JFriend`.`friend_id` = `Rating`.`user_id`)
    INNER JOIN `favred`.`categories_friends` AS `JCategoriesFriend`
        ON (`JCategoriesFriend`.`category_id` = `Rating`.`category_id`
            AND `JCategoriesFriend`.`friend_id` = `JFriend`.`id`)
    INNER JOIN `favred`.`categories` AS `JCategory`
        ON (`JCategory`.`id` = `Rating`.`category_id`
            AND `JCategory`.`id` = `JCategoriesFriend`.`category_id`)
WHERE `JFriend`.`user_id` = 1
    AND `Rating`.`user_id` <> 1
    AND `JCategory`.`id` IN (4, 14)
GROUP BY `Rating`.`id`

上面的查询不起作用,因为它没有返回任何结果(尽管表中的数据应该返回),我正在尝试做的是找到我没有创作的所有评级(ID: 1),但是由我的朋友创作,但前提是我已经选择查看该朋友的特定类别,结果集按特定的一组特定类别进行过滤。

INNER JOIN循环通过评级 - &gt;朋友 - &gt; CategoriesFreind - &gt;类别 - &gt;回到评级。

如果我删除了INNER JOIN的ON子句的附加部分,如下所示:

SELECT *
FROM `favred`.`ratings` AS `Rating` 
  INNER JOIN `favred`.`friends` AS `JFriend` 
    ON (`JFriend`.`friend_id` = `Rating`.`user_id`) 
  INNER JOIN `favred`.`categories_friends` AS `JCategoriesFriend` 
    ON (`JCategoriesFriend`.`friend_id` = `JFriend`.`id`) 
  INNER JOIN `favred`.`categories` AS `JCategory` 
    ON (`JCategory`.`id` = `JCategoriesFriend`.`category_id`) 
WHERE `JFriend`.`user_id` = 1 
  AND `Rating`.`user_id` <> 1 
  AND `JCategory`.`id` IN (4, 14) 
GROUP BY `Rating`.`id` 

然后查询将返回结果,但因为'JCategory'.'id' IN (4, 14)子句没有过滤将CategoryFriend加入评级的INNER JOIN,它会返回该朋友的所有评级,而不是按原样进行过滤。 / p>

有关如何修改我的查询以使其获取过滤结果的任何建议吗?

而且我正在使用CakePHP,因此尽管不是必需的,但首选的查询将符合其独特的查询格式。

2 个答案:

答案 0 :(得分:0)

首先,您为什么使用JFriendid,它是否意味着什么,或者它与user_id一样?

尝试这个,相同的逻辑,但它是从上到下,我觉得:

SELECT * FROM categories as JCategory
INNER JOIN categories_friends as JCategoriesFriend ON JCategoriesFriend.category_id = JCategory.id
INNER JOIN friends AS JFriend ON JFriend.friend_id = JCategoriesFriend.friend_id
INNER JOIN ratings AS Rating ON Rating.user_id = JFriend.friend_id
WHERE JCategory.id IN (4,14) AND JFriend.user_id = 1 AND Rating.user_id <> 1 GROUP BY Rating.id

我从测试的所有数据中得到了一个结果。 如果它也不起作用,尝试制作一些正确的数据,也许数据不对......

下面的测试数据:

categories: id | name (14| 141414)
categories_friends: category_id| friend_id (14| 2)
friends: id | user_id | friend_id (4| 1| 2)
ratings: id | user_id | category_id | title  (2| 2| 14 | 'haha')

答案 1 :(得分:0)

所以我想知道INNER JOINON条款中LEFT JOIN是否有点过于局限和具体。所以我想也许SELECT * FROM `favred`.`ratings` AS `Rating` INNER JOIN `favred`.`friends` AS `JFriend` ON (`JFriend`.`friend_id` = `Rating`.`user_id`) LEFT JOIN `favred`.`categories_friends` AS `JCategoriesFriend` ON (`JCategoriesFriend`.`friend_id` = `JFriend`.`id` AND `JCategoriesFriend`.`category_id` = `Rating`.`category_id`) WHERE `JFriend`.`user_id` = 1 AND `JRatingsUser`.`id` IS NULL AND `Rating`.`user_id` <> 1 GROUP BY `Rating`.`id` 会更好......

categories

该查询对我有用。

我直接链接到categories_friends表,并通过{{1}}表间接链接,这加快了查询速度,一切都运行良好。