简体   繁体   中英

Same sql query gives different results on sql server and on php output

Here is the sql query

    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;

In php is exactly the same i use codeigniter but i dont use for this active record library so is pretty much the same.

$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); 

The problem now is that in sql server i get all the results i should get although in php i dont get the results of my first two JOINS. And i have no idea why?

I hope someone knows whats going on.

In Codeigniter, run:

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

and compare to your non-CI query

Check for NULLs throughout your database that might cause problems with the joins. Other people have also had issues with PHP returning strange results as a result of NULL handling.

As alluded to by the OP, the issue is most likely to do with having the same field names in multiple tables, therefore they will get combined/overwritten when executed and returned in PHP.

Solution is to give all fields/columns that have conflicting names unique aliases. I can't think of a way to do this automatically unfortunately, so I think it would have to be a manual process.

There could also be issues with how the tables are grouped, so just in case I'll throw in a bunch of parentheses!

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

this works on me:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM