I have a query that is returning two different results from two MySql servers i'm running.
This is the query (generated by Entity Framework, I run it directly in MySql to ensure the results are coming back from each server differently):
SELECT
CASE WHEN Project1.C1 IS NULL THEN '5' ELSE Project1.data_entered END AS C1
FROM ( SELECT 1 AS X) AS SingleRowTable1
LEFT OUTER JOIN (SELECT
Extent1.data_entered,
1 AS C1
FROM rta_option_data AS Extent1
INNER JOIN rta_option_field AS Extent2 ON Extent1.rta_option_field_id = Extent2.rta_option_field_id
WHERE (Extent1.customer_id = 1546) AND (Extent2.Name = 'txtAdverseDelay') ) AS Project1 ON 1 = 1
Server A is running MySql 5.5.28 and returns 5 which is the correct and expected value.
Server B is running MySql 5.7.12-log and returns null which is the incorrect and unexpected value.
I'm at a complete loss of how to resolve this, I was planning a server upgrade this weekend but i'm super worried about other similar queries that could be impacted by this.
Additional notes:
They are both using the exact same data.
When I run the inner query on both servers they return the same results data_entered = null and C1 = null. This is the inner query i'm talking about:
SELECT
Extent1.data_entered,
1 AS C1
FROM rta_option_data AS Extent1
INNER JOIN rta_option_field AS Extent2 ON Extent1.rta_option_field_id = Extent2.rta_option_field_id
WHERE (Extent1.customer_id = 1546) AND (Extent2.Name = 'txtAdverseDelay')
Would appreciate any guidance/advice on how to possibly fix this - not sure if there is a setting in MySql or what.
Related
I have been plugging away at this and I am really at a loss for what I'm messing up. I am using My SQL workbench and sending queries to a mariadb. The original function I am working with, returns records just fine, however they pull in all of them (of which there are 1000+). Ideally the function would only return new records instead of all of them (hence no delete). I am trying to use WHERE NOT EXISTS however SQL is returning error code 1064. Sample code below:
SELECT WHERE NOT EXISTS
usr1.email
, case when ccm.course = 7 then 'CourseComplete'
when ccm.course = 10 then 'Course1' when ccm.course = 4 then 'Course1CourseComplete' else 'other' end as coursecompleted
, course
, date_add(from_unixtime(timecompleted), INTERVAL 1 HOUR) as CompletionDate
FROM moodledb.m_course_completions ccm
inner join m_user usr1
on ccm.userid = usr1.id
where timecompleted is not null and ccm.course in (4,7,10) order by 4 desc
In addition I've tried alternate placements for the where not exists function, but they all return the same error1064. Would a different operator work better? Can goku defeat this new foe? Any suggestions will be greatly appreciated
I have a select statement running in a jsp against SQL Server (previously using MySql without issues).
the TOP 1 was added because otherwise SQL Server moans about order by clauses (but only when displaying a result in a jsp, not when running the query inside SQL Server Management Studio).
This query runs fine in SQL Server Management Studio
SELECT TOP 1
alerts.id,
alerts.ts,
asset_firstname,
asset_lastname,
assetid,
alerttype.name,
node.zonename,
node.ipaddress,
node.zonegroupid
from
alerts, asset, alerttype, node, alertrules
where
ack=0 and
alerts.nodeid = node.id and
alerts.alerttypeid = alerttype.id and
alertrules.alerttypeid = alerts.alerttypeid and
alerts.assetid = asset.id and
alerts.alerttypeid = 1 and
asset.id=1157 and
alertrules.userid = 1
order by alerts.ts desc
but, when run in the jsp it returns "Column alerts.ts is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause".
I don't want alerts.ts aggregated or grouped by, hence the 'correct' select statement.
If I remove TOP 1 or alerts.ts desc the query returns the wrong row (earliest rather than latest record)
Converting what should be straightforward basic SQL commands so they run properly with SQL Server is proving a nightmare.
Any thoughts appreciated.
Regards
Ralph
(I wrote this as an answer because as a comment would be a mess)
You are using old style joins, and have redundant checks. Maybe this could make a difference (not sure, as it seems to be a problem related to JSP):
SELECT TOP(1)
alerts.id, alerts.ts,
asset_firstname,
asset_lastname,
assetid,
alerttype.name,
node.zonename,
node.ipaddress,
node.zonegroupid
from alerts
inner join asset on alerts.assetid = asset.id
inner join alerttype on alerts.alerttypeid = alerttype.id
inner join node on alerts.nodeid = node.id
inner join alertrules on alertrules.alerttypeid = alerts.alerttypeid
where ack=0 and
alerts.alerttypeid = 1 and
asset.id=1157 and
alertrules.userid = 1
order by alerts.ts desc;
In the process of upgrading from MySQL 5.6 to 5.7 on RDS I'm finding differences in results returned for a number of queries.
For instance (table names obfuscated):
SELECT `model_s`.`id`,`model_s`.`pr`,`model_s`.`o_id`,`model_s`.`uploadee`
,`model_s`.`created_at`,`model_s`.`updated_at`,`model_s`.`o_ref`,`model_s`.`i_ref`
,`model_s`.`stss`,`model_s`.`u_fol`,`model_s`.`sdd`,`model_s`.`an_t_id`,`model_s`.`rean`
,`model_s`.`dpp`,`model_s`.`bg_t_cr`
FROM `model_s`
INNER JOIN `o_o` ON (`model_s`.`o_id` = `o_o`.`id`)
WHERE (
`model_s`.`pr` IN
(
SELECT u0.`id` AS col1
FROM `pr_pr` u0
LEFT OUTER JOIN `pr_stfr_pr_p` u1 ON (u0.`id` = u1.`pr`)
LEFT OUTER JOIN `acc_strf` u2 ON (u1.`stfr_id` = u2.`id`)
LEFT OUTER JOIN `acc_us_strf_as` u3 ON (u2.`id` = u3.`stfr_id`)
LEFT OUTER JOIN `pr_us_pr_p` u5 ON (u0.`id` = u5.`pr`)
WHERE (u3.`usr` = 7 OR u5.`usr` = 7)
)
AND NOT (`model_s`.`stss` = d)
AND `o_o`.`name` LIKE %my text%
)
ORDER BY `model_s`.`created_at` DESC
In this case, 5.6 returns 1 result and 5.7 returns 2 results (the latter being the expected behaviour).
Options for the DB are merely the default RDS 5.6 and 5.7 options (none changed at all).
Any idea why with default options we see such a difference between 5.6 and 5.7? (Please, no comments on complexity/readability/optimization of the query - its generated by the Django ORM and is a question for the future...)
Going from MYSQL 5.1.73cll to MYSQL 10.1.19-MariaDB (and now running in PHP 7) this query goes from returning GOOD results to returning NO results.
SELECT t.eventid, t.tname, t.tdate, t.tyear, a.aname, t.ttour FROM
gs_tournaments as t LEFT JOIN gs_active as a ON a.eventid = t.eventid LEFT
JOIN gs_stats as s ON s.tid = t.tid WHERE s.pid = 34062 && a.active = 'y'
GROUP BY t.eventid ORDER BY t.tid ASC
I have searched other MYSQL upgrade posts but don't see an answer.
Any help would be appreciated.
Thw answer is to carefully walk back the query until you find what is breaking it, or in this case returning no results. In my case it was narrowing it down to the 'WHERE s.pid = 34062' which was a condition that was never met. This was due to the value 34062 being calculated incorrectly in another part of the code.
So lesson learned - carefully eliminate conditions or tests until you find the cuplprit. -- Ed
I've been trying to show a specific type of result in SQL but haven't been able to. I guess it's possible, but can't figure out how.
I've read this article show only those columns which have data value but couldn't solve my problem.
I have 4 tables: sparametros (holds parameters codes and descriptions), sparametrosnumericos (holds numeric parameters values and their code), sparametrostexto (holds text parameters and their values), sparametrosmemo (holds memo type parameters)
All of them can be joined by their parameter code, however, a parameter code is unique in the sense that for example, given a code, let's say 1210, and let's suppose it's a text type parameter, then that code doesn't exist in numeric nor in memo parameters either. However it exists in the general sparametros table. In other words, sparametros holds all parameters, and the other tables represent sub sets of that main set.
I've tried using left join, but couldn't get results.
This is what I have so far:
SELECT P.SPar00Id, P.SPar00Descripcion,
IF NOT ISNULL(N.SPar00NumValor) THEN
N.SPar00NumValor
ELSEIF NOT ISNULL(T.SPar00TextoValor) THEN
T.SPar00TextoValor
ELSE
M.SPar00MemoValor
FROM sparametros p
LEFT JOIN sparametrosnumericos N ON N.SPar00NumId = P.SPar00Id
LEFT JOIN sparametrostexto T ON T.SPar00TextoId = P.SPar00Id
LEFT JOIN sparametrosmemo M ON M.SPar00MemoId = P.SPar00Id
WHERE P.SIns00Id = 1 AND
N.SIns00Id = 1 AND
T.SIns00Id = 1 AND
M.SIns00Id = 1;
I'm using MySQL now (with the Navicat client), but also need to be able to get the same results in SQL Server.
The response I'm getting when executing this request is:
"[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOT ISNULL(N.SPar00NumValor) THEN "
Try this:
SELECT P.SPar00Id, P.SPar00Descripcion,
(CASE WHEN N.SPar00NumValor IS NOT NULL THEN N.SPar00NumValor
WHEN T.SPar00TextoValor IS NOT NULL THEN T.SPar00TextoValor
ELSE M.SPar00MemoValor
END) colName
FROM sparametros p
LEFT JOIN sparametrosnumericos N ON N.SPar00NumId = P.SPar00Id
LEFT JOIN sparametrostexto T ON T.SPar00TextoId = P.SPar00Id
LEFT JOIN sparametrosmemo M ON M.SPar00MemoId = P.SPar00Id
WHERE P.SIns00Id = 1 AND N.SIns00Id = 1 AND T.SIns00Id = 1 AND M.SIns00Id = 1;