0

这是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

4 回答 4

0

在 Codeigniter 中,运行:

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

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

于 2013-04-14T10:57:24.550 回答
0

检查整个数据库中可能导致连接出现问题的 NULL。由于 NULL 处理,其他人也遇到了 PHP 返回奇怪结果的问题。

于 2013-04-14T12:46:02.330 回答
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
于 2013-04-15T14:44:18.490 回答
0

这对我有用:

SET ANSI_NULLS ON 
SET QUOTED_IDENTIFIER ON 
SET CONCAT_NULL_YIELDS_NULL ON 
SET ANSI_WARNINGS ON 
SET ANSI_PADDING ON 
于 2017-04-03T08:36:36.050 回答