【问题标题】:right join query returning null value records右连接查询返回空值记录
【发布时间】:2011-10-23 19:23:23
【问题描述】:
CREATE TABLE IF NOT EXISTS `tweets_comment_tbl` (
  `tweet_comment_id` int(12) NOT NULL AUTO_INCREMENT,
  `tweet_id` int(12) NOT NULL,
  `tweets_comment` text NOT NULL,
  `created` int(12) NOT NULL,
  `changed` int(12) NOT NULL,
  `uid` int(12) NOT NULL,
  `userip` varchar(20) NOT NULL,
  `referer` text NOT NULL,
  `status` int(2) NOT NULL DEFAULT '1',
  PRIMARY KEY (`tweet_comment_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

--
-- Dumping data for table `tweets_comment_tbl`
--

INSERT INTO `tweets_comment_tbl` (`tweet_comment_id`, `tweet_id`, `tweets_comment`, `created`, `changed`, `uid`, `userip`, `referer`, `status`) VALUES
(1, 1, 'COMMENT USER1', 1319395671, 1319395671, 3, '127.0.0.1', 'http://localhost/drupal_tutorial/', 1),
(2, 2, 'comment admin user', 1319395724, 1319395724, 1, '127.0.0.1', 'http://localhost/drupal_tutorial/node', 1),
(3, 2, 'USER COMMENTING HIS COMMENT', 1319395838, 1319395838, 3, '127.0.0.1', 'http://localhost/drupal_tutorial/', 1),
(4, 2, 'ADMIN COMMENTING FOR HIS COMMENT', 1319395865, 1319395865, 1, '127.0.0.1', 'http://localhost/drupal_tutorial/node', 1),
(5, 2, 'dddCOMMENT USER1: ADMIN DOING COMMENT FOR STATUS UPDATE1', 1319395905, 1319395905, 1, '127.0.0.1', 'http://localhost/drupal_tutorial/node', 1);

我的第二张桌子

CREATE TABLE IF NOT EXISTS `tweets_tbl` (
  `tweet_id` int(11) NOT NULL AUTO_INCREMENT,
  `tweets` text NOT NULL,
  `created` int(12) NOT NULL,
  `changed` int(12) NOT NULL,
  `uid` int(12) NOT NULL,
  `userip` varchar(20) NOT NULL,
  `referer` text NOT NULL,
  `status` int(2) NOT NULL DEFAULT '1',
  PRIMARY KEY (`tweet_id`),
  KEY `tweet_id` (`tweet_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `tweets_tbl`
--

INSERT INTO `tweets_tbl` (`tweet_id`, `tweets`, `created`, `changed`, `uid`, `userip`, `referer`, `status`) VALUES
(1, 'STATUS UPDATE 1', 1319395633, 1319395633, 1, '127.0.0.1', 'http://localhost/drupal_tutorial/node', 1),
(2, 'Status update user1', 1319395696, 1319395696, 3, '127.0.0.1', 'http://localhost/drupal_tutorial/node', 1);

尝试连接查询

SELECT ut.picture as picture, tct.tweet_id as tweet_id, tct.tweets_comment as tweets_comment, tct.changed
FROM tweets_comment_tbl tct
RIGHT JOIN users as ut ON tct.uid=ut.uid AND tct.tweet_id=2 AND tct.status = 1
order by tct.created desc

返回上述查询的记录

picture         tweet_id    tweets_comment  changed
                  1         COMMENT USER1   1319395671
                NULL    NULL        NULL
picture-1.png   NULL    NULL        NULL

其实我期待的是

picture         tweet_id    tweets_comment  changed
                  1         COMMENT USER1   1319395671

为什么查询一直返回 NULL 记录,我不确定我在连接查询中哪里出错了。

【问题讨论】:

  • right outer join 按设计生成空值。如果您不想要空值(我建议您不要),请避免使用外连接。
  • 如何在没有连接的情况下完全填充以上输出

标签: mysql sql


【解决方案1】:

查询返回用户的所有行并根据ON 中的条件连接tweets_comment_tbl。如果tweets_comment_tbl 中没有记录与用户ID 匹配,则users 中的记录仍包含在记录集中。如果您只想查看使用 cmets 的用户,您可能需要 INNER JOIN。

旁注:
我认为几乎总是可以避免RIGHT JOIN;带有LEFT JOIN 的查询更容易阅读和理解。

【讨论】:

  • 我同意LEFT JOIN 比权利更容易阅读,即使它们返回相同的结果。
猜你喜欢
  • 2015-10-05
  • 1970-01-01
  • 2011-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
相关资源
最近更新 更多