交叉从 codeigniter 论坛发布,我在那里得到了蟋蟀。
我有一个查询,我使用两个左连接和几个内连接,它始终在 Firefox 中返回连接重置错误,并且在 apache 日志中没有错误消息。
如果我删除“左”参数,则不会发生相同的错误,如果我直接在数据库中从 $this->db->last_query() 运行 SQL 添加 LEFT 连接,它会正确返回(在 phpMyAdmin或命令行)。
不需要传递“内部”参数,我将它们放在那里进行实验。
$this->db->select('votes.uid AS voterUID, comments.uid AS voteeUID, voteTypes.id AS voteTypeID, userratingVoter.level AS voterLevel, userratingVotee.level AS voteeLevel');
$this->db->from('votes');
# Join the comments table on comments.id = votes.cid
$this->db->join('comments', 'comments.id = votes.cid', 'inner');
# Join the userauth table for admin WHERE clause below
$this->db->join('userauth', 'userauth.uid = votes.uid', 'inner');
# Join the votesTypes table for the WHERE IN clause below
$this->db->join('voteTypes', 'voteTypes.id = votes.voteid', 'inner');
# Join the userrating table twice once for the Voter once for the Votee
$this->db->join('userrating AS userratingVotee', 'userratingVotee.uid = comments.uid', 'left');
$this->db->join('userrating AS userratingVoter', 'userratingVoter.uid = votes.uid', 'left');
# Where in valid Vote Types
# Valid Vote Types: helpful, interesting, correct, incorrect
# 5 4 2 7
$validVoteTypes = array(5,4,2,7);
$this->db->where_in('votes.voteid', $validVoteTypes);
# Where vote is active.
$this->db->where('votes.active',1);
# Where timestamp is greater than the last run timestamp.
$this->db->where('votes.timestamp >',$lastrun);
# Only standard user votes count, may have to update this if we get other authids
$this->db->where('userauth.authid',2);
从 $this->db->last_query()... 分解 SQL 在 phpMyAdmin 和命令行中工作。
SELECT `votes`.`uid` AS voterUID, `comments`.`uid` AS voteeUID, `voteTypes`.`id` AS voteTypeID, `userratingVoter`.`level` AS voterLevel, `userratingVotee`.`level` AS voteeLevel
FROM (`votes`)
INNER JOIN `comments` ON `comments`.`id` = `votes`.`cid`
INNER JOIN `userauth` ON `userauth`.`uid` = `votes`.`uid`
INNER JOIN `voteTypes` ON `voteTypes`.`id` = `votes`.`voteid`
LEFT JOIN `userrating` AS userratingVotee ON `userratingVotee`.`uid` = `comments`.`uid`
LEFT JOIN `userrating` AS userratingVoter ON `userratingVoter`.`uid` = `votes`.`uid`
WHERE `votes`.`voteid` IN (5, 4, 2, 7)
AND `votes`.`active` = 1
AND `votes`.`timestamp` > '2012-03-01 00:00:00'
AND `userauth`.`authid` = 2
此外,如果我使用标准查询而不是活动记录语法,它会产生相同的结果。
$sql = "SELECT `votes`.`uid` AS voterUID, `comments`.`uid` AS voteeUID, `voteTypes`.`id` AS voteTypeID, `userratingVoter`.`level` AS voterLevel, `userratingVotee`.`level` AS voteeLevel FROM (`votes`) INNER JOIN `comments` ON `comments`.`id` = `votes`.`cid` INNER JOIN `userauth` ON `userauth`.`uid` = `votes`.`uid` INNER JOIN `voteTypes` ON `voteTypes`.`id` = `votes`.`voteid` LEFT JOIN `userrating` AS userratingVotee ON `userratingVotee`.`uid` = `comments`.`uid` LEFT JOIN `userrating` AS userratingVoter ON `userratingVoter`.`uid` = `votes`.`uid` WHERE `votes`.`voteid` IN (5, 4, 2, 7) AND `votes`.`active` = 1 AND `votes`.`timestamp` > '2012-03-01 00:00:00' AND `userauth`.`authid` = 2";
$query = $this->db->query($sql);
如果我使用标准 JOIN 而不是 LEFT JOIN 则不会发生错误,尽管它不会返回我需要的数据。
服务器信息:
Apache/2.2.14 (Ubuntu) PHP/5.3.2 MySQL 5.1.63 CodeIgniter 2.1.2
应用程序/配置/数据库.php
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'some_user';
$db['default']['password'] = 'some_pass';
$db['default']['database'] = 'some_db';
$db['default']['dbdriver'] = 'mysqli';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;