Click here to Skip to main content
15,921,212 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I posted this on Stack but got no traffic:

I have 2 web apps (aspx/c#) set up on two databases. The software is identical.

One database is 5.6, the other has just been upgraded to 5.7, which is when this issue arose

I have an entity framework model of the mysql databases. The query is as follows:
C#
db.ldz_from_rec.GroupBy(l => l.region, (key, g) => g.OrderBy(l=>l.LDZ_Exit).FirstOrDefault());


the result from view 'ldz_from_rec' has about 14 unique 'regions' but loads of LDZs per region. I just want one.

Here is the sql EF spits at mysql:

SQL
SELECT
`Apply1`.`id`, 
`Apply1`.`REGION1` AS `region`, 
`Apply1`.`LDZ_Id`, 
`Apply1`.`LDZ_Exit`
FROM (SELECT
`Distinct1`.`region`, 
(SELECT
`Project2`.`id`
FROM `ldz_from_rec` AS `Project2`
 WHERE (`Distinct1`.`region` = `Project2`.`region`) OR ((`Distinct1`.`region` IS  NULL) AND (`Project2`.`region` IS  NULL))
 ORDER BY 
`Project2`.`LDZ_Exit` ASC LIMIT 1) AS `id`, 
(SELECT
`Project2`.`region`
FROM `ldz_from_rec` AS `Project2`
 WHERE (`Distinct1`.`region` = `Project2`.`region`) OR ((`Distinct1`.`region` IS  NULL) AND (`Project2`.`region` IS  NULL))
 ORDER BY 
`Project2`.`LDZ_Exit` ASC LIMIT 1) AS `REGION1`, 
(SELECT
`Project2`.`LDZ_Id`
FROM `ldz_from_rec` AS `Project2`
 WHERE (`Distinct1`.`region` = `Project2`.`region`) OR ((`Distinct1`.`region` IS  NULL) AND (`Project2`.`region` IS  NULL))
 ORDER BY 
`Project2`.`LDZ_Exit` ASC LIMIT 1) AS `LDZ_Id`, 
(SELECT
`Project2`.`LDZ_Exit`
FROM `ldz_from_rec` AS `Project2`
 WHERE (`Distinct1`.`region` = `Project2`.`region`) OR ((`Distinct1`.`region` IS  NULL) AND (`Project2`.`region` IS  NULL))
 ORDER BY 
`Project2`.`LDZ_Exit` ASC LIMIT 1) AS `LDZ_Exit`
FROM (SELECT DISTINCT 
`Extent1`.`region`
FROM `ldz_from_rec` AS `Extent1`) AS `Distinct1`) AS `Apply1`


On one (5.6) database it works fine, on the other (5.7) returns null for all columns, but it does select 14 rows of nulls.

I tested the query on the databases to confirm what EF was getting. I found that in the db that didn't work, I could change the OR to and XOR and it works.

See my example:
SQL
select *,
(SELECT     `Project2`.`id`     
FROM `ldz_from_rec` AS `Project2`
/* the next line returns results */
 WHERE (`Distinct1`.`region` = `Project2`.`region`) XOR ((`Distinct1`.`region` IS  NULL) AND (`Project2`.`region` IS  NULL))
/* the next line does not return results */
 /*WHERE (`Distinct1`.`region` = `Project2`.`region`) OR ((`Distinct1`.`region` IS  NULL) AND (`Project2`.`region` IS  NULL))*/
 ORDER BY 
`Project2`.`LDZ_Exit` ASC LIMIT 1) AS `id`
from (SELECT DISTINCT 
`Extent1`.`region`
FROM `ldz_from_rec` AS `Extent1`) as `Distinct1` ;


I get that the rows are null because no rows were returned in the subqueries but how then does XOR find rows that OR could not. I cannot think of any examples where XOR can possibly return more that OR :S

This does not happen in all cases of using OR, but it is consistent in this example

Am I going mad?
Is MySql Broken?
How can I fix this?
Can I frame my lamda better to avoid this?

Thanks ^_^ Andy

What I have tried:

SQL
select *,
(SELECT     `Project2`.`id`     
FROM `ldz_from_rec` AS `Project2`
/* the next line returns results */
 WHERE (`Distinct1`.`region` = `Project2`.`region`) XOR ((`Distinct1`.`region` IS  NULL) AND (`Project2`.`region` IS  NULL))
/* the next line does not return results */
 /*WHERE (`Distinct1`.`region` = `Project2`.`region`) OR ((`Distinct1`.`region` IS  NULL) AND (`Project2`.`region` IS  NULL))*/
 ORDER BY 
`Project2`.`LDZ_Exit` ASC LIMIT 1) AS `id`
from (SELECT DISTINCT 
`Extent1`.`region`
FROM `ldz_from_rec` AS `Extent1`) as `Distinct1` ;
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900