在多种条件下加入LEFT

时间:2014-12-23 16:26:52

标签: mysql

我有以下表结构

CREATE TABLE IF NOT EXISTS `review_author` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `email` varchar(255) NOT NULL,
  `client_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `IDX_37D99F0819EB6921` (`client_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2110 ;

AND

CREATE TABLE IF NOT EXISTS `brokers_comments` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `hb_broker_id` int(11) NOT NULL,
  `client_id` int(11) DEFAULT NULL,
  `user_name` varchar(100) NOT NULL,
  `user_email` varchar(100) NOT NULL,
  `state` int(11) NOT NULL DEFAULT '0',
  `text` varchar(3000) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `IDX_5365DFFB9FE55EF7` (`hb_broker_id`),
  KEY `IDX_5365DFFB19EB6921` (`client_id`),
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1583 ;

在提取值之前,我做了以下查询:

 INSERT INTO review_author (
                    name,
                    email,
                    client_id
                )
                SELECT
                    brokers_comments.user_name,
                    brokers_comments.user_email,
                    brokers_comments.client_id
                FROM brokers_comments
                LEFT JOIN review_author
                ON brokers_comments.user_name=review_author.name AND
                brokers_comments.user_email=review_author.email AND
                brokers_comments.client_id=review_author.client_id
                WHERE  review_author.id  IS NULL

不在review_author中应该是表brokers_comments中的所有作者,现在我正在尝试使用以下查询获取作者ID:

SELECT
    review_author.id
FROM brokers_comments
LEFT JOIN review_author
    ON brokers_comments.user_name=review_author.name AND
    brokers_comments.user_email=review_author.email AND
    brokers_comments.client_id=review_author.client_id
WHERE  review_author.id  IS NOT NULL

但我从表brokers_comments的总共1531条记录中获得了大约110条结果。

更新

我无法在http://sqlfiddle.com/中插入数据,因此以下链接转储为两个表 review_author brokers_comments

我的问题再次是将不同的列(user_name,user_email,client_id)从表brokers_comments传输到表review_author,然后根据两个表中的关系名称/ email / client_id选择review_author.id。 < / p>

http://wrttn.in/7ca325 http://wrttn.in/3a7885

插入新作者错误并重复。以下是新的正确表格。

INSERT INTO review_author (
                    name,
                    email,
                    client_id
                )
                SELECT  user_name, user_email, client_id
                FROM    brokers_comments AS broker
                WHERE   NOT EXISTS
                        (
                        SELECT  1
                        FROM    review_author AS author
                        WHERE   author.email = broker.user_email
                        )
                GROUP BY broker.user_email

P.S。我有人会制作一个在线的mysql数据库,请放入评论,这样我就可以把它放在那里。

解决 直到现在我才意识到user_email必须是唯一的。基于此我做了以下select语句:

SELECT 
    author.id
FROM  brokers_comments AS broker
LEFT JOIN review_author AS author
ON broker.user_email = author.email

1 个答案:

答案 0 :(得分:1)

似乎您在JOIN子句中使用了多余的字段,因为client_id是一个键,您只需要在此字段上连接表。您获得不同记录数的可能原因是这两个表中相同client_id的名称/电子邮件不同。所以,你的两个问题应该是这样的:

 INSERT INTO review_author (
                    name,
                    email,
                    client_id
                )
                SELECT
                    brokers_comments.user_name,
                    brokers_comments.user_email,
                    brokers_comments.client_id
                FROM brokers_comments
                LEFT JOIN review_author
                ON brokers_comments.client_id=review_author.client_id
                WHERE  review_author.id  IS NULL

SELECT
    review_author.id
FROM brokers_comments
LEFT JOIN review_author
    ON brokers_comments.client_id=review_author.client_id
WHERE  review_author.id  IS NOT NULL