检索友谊的MYSQL查询只有一种方式?

时间:2013-07-16 09:56:06

标签: mysql sql union relationship

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

鉴于我们user two和我们sent的情况,user one查询请求应返回user one的帐户信息。 (SQLfiddle of this scenario)[正常工作]

鉴于我们user onerecieved来自user two的请求,查询应该返回user two的帐户信息。 (SQLfiddle of this scenario)[返回空结果]

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

我可以通过切换JOIN,LEFT => RIGHTRIGHT => LEFT来解决此问题。但这打破了第一种情况 我可以修复两个查询的联合。 (SQLfiddle of this scenario)但当然这不是最有效的方法。所以,基本问题:我该如何创建这个查询?

SCENARIO ONE:

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);

SCHEMA CREATION:

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);

1 个答案:

答案 0 :(得分:1)

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

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`;