MySQL statement failing on local machine but not deployment machine? - mysql

As part of work, I have been assigned to a massive Java EE project that has hundreds of SQL statements strewn throughout it. I set up the project on my computer and have been able to deploy the website just fine and use 99% of it without issue. I came across one odd case though, and that's when I try to create an entry on the website (its a government-based form sort of project). MySQL throws an exception when the program tries to execute the query:
SELECT d.id, ..., dol.weight, d.status
FROM DeliveryOrderEJB d, InbondEJB_DeliveryOrderEJB_link lnk
LEFT JOIN DOLineEJB dol
ON dol.id=(
SELECT MIN(dol2.id)
FROM DOLineEJB dol2
WHERE d.id=dol2.DeliveryOrderEJB_lines
)
WHERE d.id=lnk.DeliveryOrderEJB_id
AND lnk.InbondEJB_itNo='...'
ORDER BY d.id
The exception it gives me is Unknown column 'd.id' in 'where clause'
The part that I am confused about is why it seems to work fine on the production machine, and this appears to be the only one failing on my machine. The column clearly exists in the DeliveryOrderEJB table.
Thank you!

The failure is referring to the d.id in the nested subquery:
ON dol.id=(
SELECT MIN(dol2.id)
FROM DOLineEJB dol2
WHERE d.id=dol2.DeliveryOrderEJB_lines
)
You can fix the query:
SELECT d.id, ..., dol.weight, d.status
FROM DeliveryOrderEJB d join
InbondEJB_DeliveryOrderEJB_link lnk
on d.id=lnk.DeliveryOrderEJB_id left join
DOLineEJB dol
ON dol.id=(
SELECT MIN(dol2.id)
FROM DOLineEJB dol2
WHERE d.id=dol2.DeliveryOrderEJB_lines
)
WHERE lnk.InbondEJB_itNo='...'
ORDER BY d.id
I believe the problem is because you have different versions of MySQL on the machines. MySQL changed the semantics of the , around version 5.0.

Related

PHPMyadmin #1066 - Not unique table/alias error only export

I'm actually having an issue with PHPMyAdmin :
I can execute my SQL query with nice result
I get an error when I'm trying to export the same query
Here's the query :
SELECT pn.nomenclature_id as nomenclature_id, pn.lettre as lettre, a.reference as reference_article, at.nom as nom_article
FROM item a, item_translation at, item_nomenclature an, item_nomenclature pn, item p
WHERE a.type = 1
AND a.id = at.id
AND at.lang = 'fr'
AND a.id = an.item_id
AND an.nomenclature_id = pn.nomenclature_id
AND pn.item_id = p.id
AND p.type = 2
AND p.marque_id = 2
Here's a video where I had the problem : https://youtu.be/Z5AAZhoX6W0
There's lot of thread on Not unique table/alias error but I didn't found any reason to explain why the query works in PHPMyAdmin "SQL" tab but not with export.
Thanks for your support,
David.
For anyone running into this same issue and not able to apply the fix mentioned in the github issue for whatever reason (I'm working on a VPS currently where I have no access to the phpMyAdmin install).
A simple workaround is to first create a view from your query result, then go to that view and export from there. Afterwards you can drop the view again.
(to create the view just run your query but click 'create view' instead of export, fill in a name and leave the rest as is; the view will become available in the left column under the node 'Views' instead of 'Tables')

MySQL nested query won't run, runs on SQL Server

I'm running multiple queries on both MySQL and SQLServer (same queries on both servers, same db). Almost all of them run fine. I have a problem with this one:
SELECT
`Extent1`.`IdGosc`,
`Extent2`.`Imie`,
`Extent2`.`Nazwisko`
FROM `TGosc` AS `Extent1`
INNER JOIN `TOsoba` AS `Extent2` ON `Extent1`.`IdGosc` = `Extent2`.`IdOsoba`
WHERE EXISTS(
SELECT 1 AS `C1`
FROM (
SELECT `Extent3`.`IdRezerwacja`
FROM `TRezerwacja` AS `Extent3`
(here!) WHERE `Extent1`.`IdGosc` = `Extent3`.`IdGosc`) AS `Project1`
)
It runs on SQL Server just fine, returns correct results, but MySQL says:
Error Code: 1054. Unknown column 'Extent1.IdGosc' in 'where clause'.
Why so? :|
Are there any limitations about MySQL nested queries?
(Please don't offer queries that return the same and work, I can do that as well, but it's not my point)
I have seen this problem on MySQL.
SELECT `Extent1`.`IdGosc`, `Extent2`.`Imie`, `Extent2`.`Nazwisko`
FROM `TGosc` `Extent1` INNER JOIN
`TOsoba` `Extent2`
ON `Extent1`.`IdGosc` = `Extent2`.`IdOsoba`
WHERE EXISTS (SELECT `Extent3`.`IdRezerwacja`
FROM `TRezerwacja` AS `Extent3`
(here!) WHERE `Extent1`.`IdGosc` = `Extent3`.`IdGosc`
)
Fortunately, in this case, you can just eliminate the middle subquery.
I too have faced this sort of error in mysql.What I have done at tht tym is :
mysql remember only current table so try to do it may b it would work
replace
FROM `TRezerwacja` AS `Extent3
with
FROM `TRezerwacja` AS `Extent3`,`TGosc` AS `Extent1`
Ok. It turns out to be the problem with MySQL.
I'm using Entity Framework's query, that later turns into db specific SQL. I this case MySQL. So, the query in EF is:
var query3a = from TGosc gosc in context.TGosc
where gosc.TRezerwacja
.Any(x => x.TPlatnosc
.Any(y => y.Kwota > 100000))
select new { gosc.IdGosc, gosc.TOsoba.Imie, gosc.TOsoba.Nazwisko };
Now, the provider in my app is Connector NET 6.7.4. It includes MySQL.Data and MySQL.Data.Entities, both in version 6.7.4.
However, I also installed MySQL for Visual Studio 1.0.2 to be able to use more GUI than code in Visual Studio. But this thing comes with same dlls, just in different (older) versions 6.6.5. And these took precedence over the newer ones when application was running. (It's weird in the first place that in the same MySQL Installer there are two somehow conflicting versions of the same dlls.)
Anyway, I removed MySQL for Visual Studio 1.0.2, which left me with the newer dlls and see what happens to the very same LINQ to Entities query, when it's being translated to db sql:
--old 6.6.5
SELECT
Extent1.IdGosc,
Extent2.Imie,
Extent2.Nazwisko
FROM TGosc AS Extent1
INNER JOIN TOsoba AS Extent2 ON Extent1.IdGosc = Extent2.IdOsoba
WHERE EXISTS(
SELECT 1 AS C1
FROM (
SELECT Extent3.IdRezerwacja
FROM TRezerwacja AS Extent3
WHERE Extent1.IdGosc = Extent3.IdGosc) AS Project1
WHERE EXISTS(
SELECT 1 AS C1
FROM TPlatnosc AS Extent4
WHERE (Project1.IdRezerwacja = Extent4.IdRezerwacja)
AND (Extent4.Kwota > 100000)))
vs
-- new 6.7.4
SELECT
Extent1.IdGosc,
Extent2.Imie,
Extent2.Nazwisko
FROM TGosc AS Extent1
INNER JOIN TOsoba AS Extent2 ON Extent1.IdGosc = Extent2.IdOsoba
WHERE EXISTS(
SELECT 1 AS C1
FROM TRezerwacja AS Project1
WHERE EXISTS(
SELECT 1 AS C1
FROM TPlatnosc AS Extent4
WHERE (Project1.IdRezerwacja = Extent4.IdRezerwacja)
AND (Extent4.Kwota > 100000))
AND Extent1.IdGosc = Project1.IdGosc)
It's similar to what Gordon Linoff answered in this post. The middle subquery dissapears.
And of course the new query works fine!
Summing up, I guess the MySQL provider for .NET got better over these versions. I still have some queries that cause similar problems but now I think I know why that is - provider. I'm ok with that.
The annoying thing is that there are two different versions of dlls, one overriding another, in MySQL Installer. I'm using mysql-installer-community-5.6.13.0.

How do I manipulate a VARCHAR field in SQL to simplify a collection of roadmaps in a Bugzilla to Redmine migration?

As per an earlier question, I'm migrating from Bugzilla to Redmine and in doing so, I'd like to make use of the road-maps which Redmine offers.
In bugzilla, bugs were always logged against the version of software which caused the issue to be raised and although I've now preserved this information in a custom field (see the earlier question mentioned above), I now need to reduce the roadmaps down to something more manageable i.e:
Change versions of all bugs which are closed to a simplified equivalent which fits with the roadmap (e.g 0.1234 becomes 0.1 and 2.9876 becomes 2.9). This allows any one road-mapped version to have up to 999 sub-versions which is what we tended to do with Bugzilla already.
Change all bugs which are open to a new 'Unplanned' version.
Remove all of the current unused version numbers
I imagine this could be achieved with the following steps:
Getting all current versions available
For each version retrieved, strip off all but the first three characters
Check whether a version number for that product already exists.
If the version number is new, add it as a new version.
Running through every issue in the database and (a) if the issue is closed, assign it to the shortened version number or (b) if the issue is still open, assign it to a 'Unplanned' version.
...but alas, my lack of SQL knowledge is letting me down. Does anyone have any idea how to solve this?
This query gets you all the 3-character version names of all existing versions, by project:
SELECT DISTINCT v.project_id, Left(v.name, 3) newversionname
FROM issues i
INNER JOIN versions v ON i.fixed_version_id=v.id
You'll need a list of all 3-character version names which do not yet exist:
SELECT f.project_id, f.newversionname
FROM (
SELECT DISTINCT v.project_id, Left(v.name, 3) newversionname
FROM issues i INNER JOIN versions v ON i.fixed_version_id=v.id
) f
LEFT OUTER JOIN versions v2 ON f.project_id = v2.project_id and f.newversionname=v2.name
WHERE v2.project_id is null
You'll need to insert new version for each result of the above query (I'll leave the adaptation of the above query as an INSERT query to you...).
EDIT: Updated query to add version details
SELECT f.project_id, f.newversionname, v3.description, v3.created_on, v3.updated_on
FROM (
SELECT v.project_id, Left(v.name, 3) newversionname, MIN(v.id) minversionid
FROM issues i INNER JOIN versions v ON i.fixed_version_id=v.id
GROUP BY v.project_id
) f
LEFT OUTER JOIN versions v2 ON f.project_id = v2.project_id and f.newversionname=v2.name
INNER JOIN versions v3 ON f.minversionid=v3.id
WHERE v2.project_id is null
This will simply select the details of the version with the lowest id for each new version.
Note: This will break if you have version bigger than 9 (ie 11.234 becomes 11.).
Now we now that for every issue associatted with an old version, there exists a new 3-character version. The following query shows which one:
SELECT DISTINCT i.id, v.id oldversionid, v.name oldversionname, v2.id newversionid, v2.name newversionname
FROM issues i
INNER JOIN versions v ON i.fixed_version_id=v.id
INNER JOIN versions v2 ON LEFT(v.name, 3) = v2.name and v.project_id = v2.project_id
WHERE v.id <> v2.id
You may use this query and adapt it as an UPDATE query after a sanity check. You'll still need to add the criteria to distinguish between the open and closed issues (status_id).
Hope this helps with your migration, have fun with redmine!

Mysql 4.0.30 Error 1046... but working on 5.5!

On our dev-server we've got MySql version 5.5, but on production server (from aruba.it) we've Mysql 4.0.30.
The SQL query that cause problem is:
SELECT r.iduser,ra.desapp,
r.cognom,f.desfig, r.fl_dip, (Select
sum(o.numore)/8 From OreConsuntive o,
Prjtask pt, Prjprogetti pp, Prjordini
po Where o.IDuser = '54'and o.dt_cns >= '20110101' and
o.dt_cns <= '20110131' and o.codcop='F00' and
o.codtsk=pt.codtsk and
pt.codprj=pp.codprj and
pp.codord=po.codord and
left(po.cliatt,3)<>'CSA' group by
o.numore) as TOTGIORNIFATTMESE FROM
Risorse r, Figure f, RisorsaArea ra
WHERE r.codfig=f.codfig AND
r.areapp=ra.codapp AND r.IDuser='54'
ORDER BY r.areapp desc
I'll try to execute only this part
Select sum(o.numore)/8
From OreConsuntive o, Prjtask pt,
Prjprogetti pp, Prjordini po Where
o.IDuser = '54'and o.dt_cns >=
'20110101' and o.dt_cns <= '20110131'
and o.codcop='F00' and
o.codtsk=pt.codtsk and
pt.codprj=pp.codprj and
pp.codord=po.codord and
left(po.cliatt,3)<>'CSA' group by
o.numore
and it seems to work!
Can the problem be caused by the annidated select ?
How con I modify my query to get a working one on mysql 4.0.30 ?
Thanks a lot!
Davide
MySQL versions prior to 4.1 do not support subqueries. You will have to rewrite your query to avoid that.
Unfortunately, I can't easily tell how to do that in your case, but try reading http://dev.mysql.com/doc/refman/4.1/en/rewriting-subqueries.html - it may help you to rewrite it.
I don't know your particular host, but if they cannot upgrade you to a newer MySQL, you should try to downgrade your development server to run the same version as your production server - a lot of things have changed since 4.0, and catching them early might be a good idea.

Strange Mysql error 1111, supposedly worked before

I am trying to troubleshoot a particular 'report' that gets generated on a PHP application running on a VOIP platform (billing related). The person that asked me to do this stated "it worked before" ;)
MySQL Error thrown:
1111: Invalid use of group function
Crazy thing is, it 'worked before' but stopped after awhile, they cannot place what event took place that might have created this bug. One thought is maybe MYSQL was upgraded? I googled the error, and can't seem to find a clear answer.
At first glance do you guys see anything wrong here in the SQL?
SELECT C.ResourceGroupID AS CustomerID, CS.CustomerName, SUM(C.IN_RndDuration/60) AS Minutes, SUM(CASE WHEN (C.OUT_Duration>0 AND C.IN_RndDuration>0) THEN 1 ELSE 0 END) AS Successfull, count(0) AS Attempts
FROM ws_call_current AS C
LEFT JOIN customer_rg AS V_RG ON
V_RG.RGId=C.OtherResourceGroupID AND V_RG.RateTableId IS NOT NULL AND V_RG.Direction='O'
LEFT JOIN customer AS V ON V.CustomerId=V_RG.CustomerId
LEFT JOIN customer_rg AS CS_RG ON CS_RG.RGId=C.ResourceGroupID AND CS_RG.RateTableId IS NOT NULL AND CS_RG.Direction='I'
LEFT JOIN customer AS CS ON CS.CustomerId=CS_RG.CustomerId
WHERE DATE_FORMAT(DATE_ADD(C.SeizeDate, INTERVAL TIME_TO_SEC(C.SeizeTime) SECOND), '%Y-%m-%d %H:%i:00') BETWEEN '2010-10-19 00:00:00' AND '2010-10-19 23:59:59'
AND C.SeizeDate BETWEEN '2010-10-19' AND '2010-10-19'
GROUP BY CS.CustomerName
ORDER BY SUM(C.IN_RndDuration/60) DESC
This is written for MYSQL 3/4 I believe, not yet sure. Just wanted to get some feedback.
From google results I find some people had success with fixing this by modifying the query to be a Having instead of a where?
Not sure. Anything look odd below?
Try including C.ResourceGroupID in your group by statement. You need to include all non-aggregate items in your select in your group by.
You shouldn't need a having in the code above.
I'm not sure how it could have worked before...
What I found after review is that the cause of this was simply a MySQL discrepancy. Initially the MySQL code was written for 5.0.51a, and the running MySQL version (it was somehow 'attempted' to be upgraded, was 5.0.21).
This version 'downgrade' was the cause of the failed SQL. I honestly don't know what the cause was other than we installed a new version of MySQL to just do a test (on a remote box) and it solved the issue (pointing from primary box to backup MySQL server.
Solution for this error: Version compatibility