相同的SQL查询在sql server和php输出上给出了不同的结果

时间:2013-04-14 10:10:01

标签: php sql codeigniter

这是sql查询

    SELECT * FROM seedbed
LEFT OUTER JOIN fertilizers AS seedfert ON seedfert.fert_protocol =seedbed.seed_protocol
LEFT OUTER JOIN pesticides AS seedpest ON seedpest.pest_protocol = seedbed.seed_protocol
LEFT OUTER JOIN fertilizers_list AS seedfert_list ON  seedfert.fert_id =seedfert_list.id    
LEFT OUTER JOIN pesticides_list AS seedpest_list ON  seedpest.pest_id = seedpest_list.id
LEFT OUTER JOIN field ON field.field_protocol = seedbed.seed_protocol + "%"         
LEFT OUTER JOIN fertilizers AS fieldfert ON fieldfert.fert_protocol = field.field_protocol
LEFT OUTER JOIN pesticides AS fieldpest ON fieldpest.pest_protocol = field.field_protocol
LEFT OUTER JOIN stock ON stock.field_protocol = field.field_protocol
LEFT OUTER JOIN fertilizers_list AS fieldfert_list ON  fieldfert.fert_id =fieldfert_list.id 
LEFT OUTER JOIN pesticides_list AS fieldpest_list ON  fieldpest.pest_id = fieldpest_list.id
WHERE seedbed.seed_protocol LIKE "1/2013/2%"
ORDER BY field_num;

在php中我使用codeigniter完全相同,但我不使用这个活动记录库,所以几乎是一样的。

$sql = "SELECT * FROM seedbed
LEFT OUTER JOIN fertilizers AS seedfert ON seedfert.fert_protocol = seedbed.seed_protocol
LEFT OUTER JOIN pesticides AS seedpest ON seedpest.pest_protocol = seedbed.seed_protocol
LEFT OUTER JOIN fertilizers_list AS seedfert_list ON  seedfert.fert_id = seedfert_list.id   
LEFT OUTER JOIN pesticides_list AS seedpest_list ON  seedpest.pest_id = seedpest_list.id
LEFT OUTER JOIN field ON field.field_protocol = seedbed.seed_protocol + '%'         
LEFT OUTER JOIN fertilizers AS fieldfert ON fieldfert.fert_protocol = field.field_protocol
LEFT OUTER JOIN pesticides AS fieldpest ON fieldpest.pest_protocol = field.field_protocol
LEFT OUTER JOIN stock ON stock.field_protocol = field.field_protocol
LEFT OUTER JOIN fertilizers_list AS fieldfert_list ON  fieldfert.fert_id =fieldfert_list.id 
LEFT OUTER JOIN pesticides_list AS fieldpest_list ON  fieldpest.pest_id = fieldpest_list.id
WHERE seedbed.seed_protocol LIKE '".$this->db->escape_like_str($ins_protocol)."%'
ORDER BY field_num";
$query = $this->db->query($sql);
$data = $query->result();
print_r($data); 

现在的问题是在sql server中我得到了我应该得到的所有结果虽然在php中我没有得到我的前两个JOINS的结果。我不明白为什么?

我希望有人知道最新情况。

4 个答案:

答案 0 :(得分:0)

在Codeigniter中,运行:

echo $this->db->last_query();

并与您的非CI查询进行比较

答案 1 :(得分:0)

检查整个数据库中是否存在可能导致联接问题的NULL。由于NULL处理,Other people也遇到了PHP返回奇怪结果的问题。

答案 2 :(得分:0)

正如OP提到的那样,问题很可能与在多个表中具有相同的字段名称有关,因此它们在执行并在PHP中返回时将被合并/覆盖。

解决方案是为所有具有冲突名称的字段/列提供唯一别名。遗憾的是,我无法想到一种自动执行此操作的方法,所以我认为这必须是一个手动过程。

表格如何分组也可能存在问题,所以万一我会抛出一堆括号!

SELECT seedbed.*, 
       seedfert.field1 AS seedfert_field1, seedfert.field2 AS seedfert_field2, 
       seedpest.field1 AS seedpest_field1, seedpest.field2 AS seedpest_field2, 
       seedfert_list.field1 AS seedfert_list_field1, seedfert_list.field2 AS seedfert_list_field2, 
       seedpest_list.field1 AS seedpest_list_field1, seedpest_list.field2 AS seedpest_list_field2, 
       etc...
FROM (((((((((seedbed
    LEFT OUTER JOIN fertilizers AS seedfert ON seedfert.fert_protocol = seedbed.seed_protocol)
    LEFT OUTER JOIN pesticides AS seedpest ON seedpest.pest_protocol = seedbed.seed_protocol)
    LEFT OUTER JOIN fertilizers_list AS seedfert_list ON  seedfert.fert_id = seedfert_list.id)
    LEFT OUTER JOIN pesticides_list AS seedpest_list ON  seedpest.pest_id = seedpest_list.id)
    LEFT OUTER JOIN field ON field.field_protocol = seedbed.seed_protocol + '%')
    LEFT OUTER JOIN fertilizers AS fieldfert ON fieldfert.fert_protocol = field.field_protocol)
    LEFT OUTER JOIN pesticides AS fieldpest ON fieldpest.pest_protocol = field.field_protocol)
    LEFT OUTER JOIN stock ON stock.field_protocol = field.field_protocol)
    LEFT OUTER JOIN fertilizers_list AS fieldfert_list ON  fieldfert.fert_id =fieldfert_list.id)
    LEFT OUTER JOIN pesticides_list AS fieldpest_list ON  fieldpest.pest_id = fieldpest_list.id
WHERE seedbed.seed_protocol LIKE '".$this->db->escape_like_str($ins_protocol)."%'
ORDER BY field_num

答案 3 :(得分:0)

这适用于我:

SET ANSI_NULLS ON 
SET QUOTED_IDENTIFIER ON 
SET CONCAT_NULL_YIELDS_NULL ON 
SET ANSI_WARNINGS ON 
SET ANSI_PADDING ON