我很难搞清楚这个。我正在尝试建立一个会话系统,它会像这样工作:
登录用户将拥有指向“对话”的链接。在该页面上将显示所有conversation.titles的列表。当点击会话名称时,会话消息(主要在2个用户之间,但可能更多)将显示如下:
| ---------------------------------------------- -------------- |
|发送键|这里有新消息的文本字段|
| ---------------- | -------------------------------- ----------- |
接收者:嗨!.....(08/24/11 - 11PM
发送者:bla blah ...(08/24/11 - 10PM)等。
我在对话本身中列出消息没有问题,而是获取用户参与的对话列表。
我得到同一个实例的多个结果。
我想要一个查询,通过发送消息,接收消息或两者来生成已登录用户参与的所有会话的列表。
我想我需要一个嵌套查询,但我不知道该怎么做。所以我有点卡在这里,尝试内部连接所有的桌子都不会切奶酪。
这是database mysql设计/ ER-modell:
-- -----------------------------------------------------
-- Table `firm`.`conversation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `firm`.`conversation` (
`conversation_id` INT NOT NULL AUTO_INCREMENT ,
`title` VARCHAR(45) NULL ,
`date` TIMESTAMP NULL ,
PRIMARY KEY (`subject_id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `firm`.`message`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `firm`.`message` (
`message_id` INT NOT NULL AUTO_INCREMENT ,
`message` LONGTEXT NULL ,
`date` TIMESTAMP NOT NULL ,
`read_sender` BINARY NOT NULL ,
`read_receiver` BINARY NOT NULL ,
`deleted_receiver` BINARY NOT NULL ,
`deleted_sender` BINARY NOT NULL ,
`conversation_id` INT NOT NULL ,
PRIMARY KEY (`message_id`) ,
INDEX `fk_message_subject1` (`subject_id` ASC) ,
CONSTRAINT `fk_message_con1`
FOREIGN KEY (`conversation_id` )
REFERENCES `firm`.`conversation` (`conversation_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;
-- -----------------------------------------------------
-- Table `firm`.`outbox`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `firm`.`outbox` (
`outbox_id` INT NOT NULL AUTO_INCREMENT ,
`user_id` INT(10) NOT NULL ,
`conversation_id` INT(10) NOT NULL ,
`date` TIMESTAMP NOT NULL ,
PRIMARY KEY (`outbox_id`) ,
INDEX `fk_outbox_users1` (`user_id` ASC) ,
INDEX `fk_utbox_con1` (`conversation_id` ASC) ,
CONSTRAINT `fk_outbox_users1`
FOREIGN KEY (`user_id` )
REFERENCES `firm`.`users` (`user_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_outbox_con1`
FOREIGN KEY (`conversation_id` )
REFERENCES `firm`.`conversation` (`conversation_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
-- -----------------------------------------------------
-- Table `firm`.`inbox`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `firm`.`inbox` (
`inbox_id` INT NOT NULL ,
`user_id` INT(10) NOT NULL ,
`message_id` INT NOT NULL ,
`read` BINARY NOT NULL ,
`date` TIMESTAMP NOT NULL ,
PRIMARY KEY (`inbox_id`) ,
INDEX `fk_inbox_users1` (`user_id` ASC) ,
INDEX `fk_inbox_message` (`message_id` ASC) ,
CONSTRAINT `fk_inbox_users1`
FOREIGN KEY (`user_id` )
REFERENCES `firm`.`users` (`user_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_inbox_message1`
FOREIGN KEY (`message_id` )
REFERENCES `frim`.`message` (`message_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
答案 0 :(得分:0)
SELECT DISTINCT conversation.conversation_id
FROM conversation
INNER JOIN message ON message.conversation_id = conversation.conversation_id
INNER JOIN inbox ON inbox.message_id = message.message_id
INNER JOIN outbox ON inbox.message_id = message.message_id
WHERE outbox.user_id = #USER#
OR inbox.user_id = #USER#
此联接应该有效。 DISTINCT
因为用户可能已作为发件人和收件人参与。