1

我有一个查询应该是检索用户的朋友。

鉴于我们所处的情况,我们user two对查询sent的请求user one应返回 的帐户信息user one。(这种情况的 SQLfiddle)[正常工作]

鉴于我们的情况,我们user one来自recieved查询的请求user two应该返回 的帐户信息user two。(本场景的 SQLfiddle)[返回空结果]

因此,只要我们是发送请求的用户,查询就可以正常工作,但是如果我们是接收请求的用户,它就不起作用。

我可以通过切换 JOIN、LEFT => RIGHT、来解决这个问题RIGHT => LEFT。但这打破了第一种情况。
我可以解决两个查询的联合问题。(这个场景的 SQLfiddle)但肯定不是最有效的方法。所以,基本问题:我应该如何创建这个查询?

情景一:

SELECT `accounts`.*,
  IFNULL(`f1`.`sender`,        `f2`.`sender`)        AS `sender`,
  IFNULL(`f1`.`recipient`,     `f2`.`recipient`)     AS `recipient`,
  IFNULL(`f1`.`date_sent`,     `f2`.`date_sent`)     AS `date_sent`,
  IFNULL(`f1`.`date_reviewed`, `f2`.`date_reviewed`) AS `date_reviewed`,
  IFNULL(`f1`.`accepted`,      `f2`.`accepted`)      AS `accepted` 
FROM `accounts` 
  LEFT  JOIN `friendships` AS `f1` ON `accounts`.`id` = `f1`.`sender` 
  RIGHT JOIN `friendships` AS `f2` ON `accounts`.`id` = `f2`.`recipient` 
WHERE (`f1`.`recipient` = 2 OR `f2`.`sender` = 2);

修补场景二

SELECT `accounts`.*,
  IFNULL(`f1`.`sender`,        `f2`.`sender`)        AS `sender`,
  IFNULL(`f1`.`recipient`,     `f2`.`recipient`)     AS `recipient`,
  IFNULL(`f1`.`date_sent`,     `f2`.`date_sent`)     AS `date_sent`,
  IFNULL(`f1`.`date_reviewed`, `f2`.`date_reviewed`) AS `date_reviewed`,
  IFNULL(`f1`.`accepted`,      `f2`.`accepted`)      AS `accepted` 
FROM `accounts` 
  RIGHT JOIN `friendships` AS `f1` ON `accounts`.`id` = `f1`.`sender` 
  LEFT  JOIN `friendships` AS `f2` ON `accounts`.`id` = `f2`.`recipient` 
WHERE (`f1`.`recipient` = 2 OR `f2`.`sender` = 2);

为两种情况工作(问题基本上是;没有工会我怎么能做到这一点?):

SELECT `accounts`.*,
  IFNULL(`f1`.`sender`,        `f2`.`sender`)        AS `sender`,
  IFNULL(`f1`.`recipient`,     `f2`.`recipient`)     AS `recipient`,
  IFNULL(`f1`.`date_sent`,     `f2`.`date_sent`)     AS `date_sent`,
  IFNULL(`f1`.`date_reviewed`, `f2`.`date_reviewed`) AS `date_reviewed`,
  IFNULL(`f1`.`accepted`,      `f2`.`accepted`)      AS `accepted` 
FROM `accounts` 
  LEFT  JOIN `friendships` AS `f1` ON `accounts`.`id` = `f1`.`sender` 
  RIGHT JOIN `friendships` AS `f2` ON `accounts`.`id` = `f2`.`recipient` 
WHERE (`f1`.`recipient` = 1 OR `f2`.`sender` = 1)
UNION
SELECT `accounts`.*,
  IFNULL(`f1`.`sender`,        `f2`.`sender`)        AS `sender`,
  IFNULL(`f1`.`recipient`,     `f2`.`recipient`)     AS `recipient`,
  IFNULL(`f1`.`date_sent`,     `f2`.`date_sent`)     AS `date_sent`,
  IFNULL(`f1`.`date_reviewed`, `f2`.`date_reviewed`) AS `date_reviewed`,
  IFNULL(`f1`.`accepted`,      `f2`.`accepted`)      AS `accepted` 
FROM `accounts` 
  RIGHT  JOIN `friendships` AS `f1` ON `accounts`.`id` = `f1`.`sender` 
  LEFT JOIN `friendships` AS `f2` ON `accounts`.`id` = `f2`.`recipient` 
WHERE (`f1`.`recipient` = 1 OR `f2`.`sender` = 1);

架构创建:

CREATE TABLE `accounts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `status` tinyint(4) DEFAULT '0',
  `type` varchar(25) NOT NULL,
  `username` varchar(25) NOT NULL,
  `first_name` varchar(80) NOT NULL,
  `last_name` varchar(80) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

/*Data for the table `accounts` */

insert  into `accounts`
(`id`,`status`,`type`,`username`,`first_name`,`last_name`) 
values 
(1,1,'writer','user1','User','One'),
(2,1,'writer','user2','User','Two'),
(3,1,'publisher','user3','User','Three');

CREATE TABLE `friendships` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `sender` int(11) NOT NULL,
  `recipient` int(11) NOT NULL,
  `date_sent` datetime DEFAULT NULL,
  `date_reviewed` datetime DEFAULT NULL,
  `accepted` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

/*Data for the table `friendships` */

insert  into `friendships`
(`id`,`sender`,`recipient`,`date_sent`,`date_reviewed`,`accepted`) 
values (3,2,1,'2013-07-16 20:54:46',NULL,NULL);
4

1 回答 1

1

请试试这个查询。我删除了两个连接。并且只需要一个连接,双方都可以工作。

SELECT `accounts`.*, `f1`.`sender`  AS `sender`,`f1`.`recipient`  AS `recipient`,`f1`.`date_sent`  AS `date_sent`,  `f1`.`date_reviewed` AS `date_reviewed`,`f1`.`accepted`    AS `accepted` FROM `accounts`   JOIN `friendships` AS `f1` ON (`accounts`.`id` = `f1`.`sender`   or `accounts`.`id` = `f1`.`recipient`) WHERE    if(`f1`.`recipient` = 1, `f1`.`sender` , if(`f1`.`sender` = 1, `f1`.`recipient` , 0)) = `accounts`.`id`;
于 2013-07-16T11:04:51.533 回答