当我使用Rails" ActiveRecord :: Base.connection.execute"方法,此结果将返回56个数据,数据正常,与数据库中的查询相同:
db = ActiveRecord::Base.connection
sql = "select u.id, u.name, u.email from users as u join
(select d.user_id from testthrees as d
left join testtwos as m on d.test_name = m.test_name
and d.test_value = m.test_value
where m.testone_id in (1)
) as ma
where ma.user_id = u.id
group by u.id
order by u.id desc"
db.execute(sql)
但是当我使用Ruby DBI时,这个结果会返回54个数据,实际上这个结果应该返回56个数据,结果少了两个用户数据:
db = DBI.connect("DBI:Mysql:Test:localhost", "testuser", "testpassword")
sql = "select u.id, u.name, u.email from users as u join
(select d.user_id from testthrees as d
left join testtwos as m on d.test_name = m.test_name
and d.test_value = m.test_value
where m.testone_id in (?)
) as ma
where ma.user_id = u.id
group by u.id
order by u.id desc"
dbh = db.prepare(sql)
dbh.execute(1)
这是关系表:
//Table: users
CREATE TABLE IF NOT EXISTS `users` (
`id` int(6) unsigned NOT NULL,
`name` varchar(100) NOT NULL,
`email` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `users` (`id`, `name`, `email`) VALUES
('1', 'aa', 'aa@gmail.com'),
('2', 'bb', 'bb@gmail.com'),
('3', 'cc', 'cc@gmail.com'),
('4', 'dd', 'dd@gmail.com'),
('5', 'ee', 'ee@gmail.com'),
('6', 'ff', 'ff@gmail.com'),
('7', 'gg', 'gg@gmail.com'),
('8', 'ii', 'ii@gmail.com'),
('9', '11', '11@gmail.com'),
('10', '22', '22@gmail.com'),
('11', '33', '33@gmail.com'),
('12', '44', '44@gmail.com'),
('13', '55', '55@gmail.com'),
('14', '66', '66@gmail.com'),
('15', '77', '77@gmail.com'),
('16', '88', '88@gmail.com');
//Table: testones
CREATE TABLE IF NOT EXISTS `testones` (
`id` int(6) unsigned NOT NULL,
`one_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `testones` (`id`, `one_name`) VALUES
('1', 'haha'),
('2', 'hehe'),
('3', 'lala'),
('4', 'yaya');
//Table: testtwos
CREATE TABLE IF NOT EXISTS `testtwos` (
`id` int(6) unsigned NOT NULL,
`testone_id` int(6) unsigned NOT NULL,
`test_name` varchar(100) NOT NULL,
`test_value` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `testtwos` (`id`, `testone_id`, `test_name`, `test_value`) VALUES
('1', '1', 'name1', 'value1'),
('2', '1', 'name2', 'value2'),
('3', '2', 'name3', 'value3'),
('4', '2', 'name4', 'value4'),
('5', '3', 'name5', 'value5'),
('6', '3', 'name6', 'value6'),
('7', '4', 'name7', 'value7'),
('8', '4', 'name8', 'value8'),
('9', '1', 'name9', 'value9'),
('10', '2', 'name10', 'value10'),
('11', '2', 'name11', 'value11'),
('12', '3', 'name12', 'value12'),
('13', '1', 'name13', 'value13');
//Table: testthrees
CREATE TABLE IF NOT EXISTS `testthrees` (
`id` int(6) unsigned NOT NULL,
`user_id` int(6) unsigned NOT NULL,
`test_name` varchar(100) NOT NULL,
`test_value` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `testthrees` (`id`, `user_id`, `test_name`, `test_value`) VALUES
('1', '1', 'name1', 'value1'),
('2', '1', 'name2', 'value2'),
('3', '2', 'name1', 'value1'),
('4', '5', 'name3', 'value3'),
('5', '7', 'name3', 'value3'),
('6', '7', 'name4', 'value4'),
('7', '7', 'name10', 'value10'),
('8', '10', 'name11', 'value11'),
('9', '14', 'name12', 'value12'),
('10', '13', 'name1', 'value1');