左连接不符合我的要求

时间:2015-04-18 12:02:07

标签: mysql

这里有两个表格:第一个是"民意调查"和 第二个回答"回答"。我的要求是这样的:

  

我希望仅针对没有回复投票的用户显示投票记录

我尝试过此查询,但无效:

select p.* from mycom_poll as p 
    LEFT JOIN mycom_polls_result as pr on (p.b_id != pr.b_poll_id
        And pr.b_user_id !=14) where p.v_status = 'enable' 
                And p.v_country like '%India%'
                AND p.d_activate_date <= '2015-04-17' order by p.b_id limit 1"

    CREATE TABLE IF NOT EXISTS `mycom_poll` (
      `b_id` bigint(20) NOT NULL AUTO_INCREMENT,
      `b_user_id` bigint(20) NOT NULL DEFAULT '0',
      `v_code` varchar(50) NOT NULL,
      `v_question` varchar(1000) CHARACTER SET utf8 NOT NULL,
      `v_country` varchar(100) NOT NULL,
      `d_activate_date` date NOT NULL,
      `dt_created_date` datetime NOT NULL,
      `dt_updated_date` datetime NOT NULL,
      `v_ip` varchar(20) NOT NULL,
      `v_status` varchar(20) NOT NULL DEFAULT 'enable',
      PRIMARY KEY (`b_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

    INSERT INTO `mycom_poll` (`b_id`, `b_user_id`, `v_code`, `v_question`, `v_country`, `d_activate_date`, `dt_created_date`, `dt_updated_date`, `v_ip`, `v_status`) VALUES(8, 0, '20150417115406-192168113C5ZJpXu2hM', 'Who is chief minstor of Gujarat in 2015 Rajyasabha?', '["Global","India","United States"]', '2015-04-15', '2015-04-17 11:56:12', '2015-04-18 07:29:11', '127.0.0.1', 'enable');

    CREATE TABLE IF NOT EXISTS `mycom_polls_result` (
      `b_id` bigint(20) NOT NULL AUTO_INCREMENT,
      `b_poll_id` bigint(20) NOT NULL,
      `b_user_id` bigint(20) NOT NULL,
      `b_poll_answer` bigint(20) NOT NULL,
      `dt_created_date` datetime NOT NULL,
      `dt_updated_date` datetime NOT NULL,
      `v_ip` varchar(20) NOT NULL,
      `v_status` varchar(20) NOT NULL DEFAULT 'enable',
      PRIMARY KEY (`b_id`),
      KEY `b_poll_id` (`b_poll_id`,`b_user_id`),
      KEY `b_poll_id_2` (`b_poll_id`,`b_user_id`,`b_poll_answer`),
      KEY `b_poll_id_3` (`b_poll_id`,`b_user_id`,`b_poll_answer`),
      KEY `b_user_id` (`b_user_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

    INSERT INTO `mycom_polls_result` (`b_id`, `b_poll_id`, `b_user_id`, `b_poll_answer`, `dt_created_date`, `dt_updated_date`, `v_ip`, `v_status`) VALUES
    (1, 8, 14, 64, '2015-04-18 13:27:50', '2015-04-18 13:27:50', '127.0.0.1', 'enable');

2 个答案:

答案 0 :(得分:0)

目前您正在使用LEFT JOIN来强制强制mycom_poll表的所有行,但您正在加入p.b_id != pr.b_poll_id,这将关联每个投票行在mycom_poll中,mycom_polls_result中所有不相关的结果行都不属于它。

您可能需要加入p.b_id = pr.b_poll_id,然后在pr.b_poll_id IS NULL子句中要求WHERE行。

您似乎也试图将结果限制为pr.b_user_id !=14,这很好,但需要与WHERE子句中的其他过滤条件一起说明。 LEFT JOIN ON子句将用于声明表格之间的列关系。

    SELECT p.*
     FROM mycom_poll as p 
     LEFT JOIN mycom_polls_result as pr
     ON p.b_id = pr.b_poll_id
    WHERE
     pr.b_user_id !=14
     AND p.v_status = 'enable' 
     AND p.v_country like '%India%'
     AND p.d_activate_date <= '2015-04-17' 
     AND pr.b_poll_id IS NULL
    ORDER BY p.b_id LIMIT 1

HTH

答案 1 :(得分:0)

您的查询:

select p.* from mycom_poll as p 
        LEFT JOIN mycom_polls_result as pr on (p.b_id != pr.b_poll_id
            And pr.b_user_id !=14) where p.v_status = 'enable' 
                    And p.v_country like '%India%'
                    AND p.d_activate_date <= '2015-04-17' order by p.b_id limit 1

首先我要指出这个查询的错误,你在这里做的是,你要加入第二个表的 id的两个表,而不是第二个表的id ,这将是加入其id不相同的所有表数据,当你进行左连接时,你总是得到左表的数据,如果右表中没有数据,你得到null。

所以在 where子句中,你应该检查第二个表的字段是否为null或者不是这样的。

select p.* from mycom_poll as p 
    LEFT JOIN mycom_polls_result as pr on p.b_id = pr.b_poll_id
    where pr.b_user_id !=14 p.v_status = 'enable' 
    And p.v_country like '%India%' AND p.d_activate_date <= '2015-04-17'
    and pr.b_poll_id is null order by p.b_id limit 1

我先请你理解left join