943,922 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Marked Solved
  • Views: 1752
  • MySQL RSS
Dec 1st, 2008
0

Converting queries from MySQL 4 to MySQL 5

Expand Post »
OK .. I was migrating an application from a staging server where the MySQL version is 4.1.10a to a live server where the MySQL version is 5.0.45. There apparently is quite a difference between query syntax on one versus the other.

For example, in version 4, this worked:

MySQL Syntax (Toggle Plain Text)
  1. SELECT uas.usr_id_users, uas.airport_id_airport, a.airport_code, a.airport_name, a.airport_id, MAX(if(service_id_service=1, 1, 0)) AS JET, MAX(if(service_id_service=2, 1, 0)) AS GROUND, MAX(if(service_id_service=3, 1, 0)) AS GLYCOL, u.usr_fname AS FIRSTNAME, u.usr_lname AS LASTNAME, u.usr_email AS EMAIL
  2. FROM userairportservices uas, users u LEFT JOIN airport a ON a.airport_id = uas.airport_id_airport
  3. WHERE uas.usr_id_users = usr_id
  4. GROUP BY airport_id_airport LIMIT 0, 30

However, on version 5 it does not.

Here is what I had to do to make it work:

MySQL Syntax (Toggle Plain Text)
  1. SELECT uas.usr_id_users, uas.airport_id_airport, a.airport_code, a.airport_name, a.airport_id, MAX( if( service_id_service =1, 1, 0 ) ) AS JET, MAX( if( service_id_service =2, 1, 0 ) ) AS GROUND, MAX( if( service_id_service =3, 1, 0 ) ) AS GLYCOL, u.usr_fname AS FIRSTNAME, u.usr_lname AS LASTNAME, u.usr_email AS EMAIL
  2. FROM users u LEFT JOIN userairportservices uas ON uas.usr_id_users =usr_id LEFT JOIN airport a ON a.airport_id = uas.airport_id_airport
  3. GROUP BY airport_id_airport LIMIT 0 , 30

As an example, here is another query I had that did not work in VER: 5:

MySQL Syntax (Toggle Plain Text)
  1. SELECT usr_id, usr_access, usr_title, usr_fname, usr_lname, usr_email, access.access, title.title
  2. FROM users INNER JOIN access, title ON access_id = usr_access AND title_id = usr_title

But, it was suggested by dickersonka to try the following, which worked:

MySQL Syntax (Toggle Plain Text)
  1. SELECT u.usr_id, u.usr_access, u.usr_title, u.usr_fname, u.usr_lname, u.usr_email, a.access, t.title
  2. FROM users u INNER JOIN access a ON a.access_id = u.usr_access INNER JOIN title t ON t.title_id = u.usr_title

So I post this here not to ask for help, which I have received plenty of here, but in the hopes this helps someone else who runs into this issue.

Cheers,

Dave
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster
filch is offline Offline
132 posts
since Nov 2008
Dec 1st, 2008
1

Re: Converting queries from MySQL 4 to MySQL 5

here i found an article on the suggestion i gave you earlier

its because order of precedence, also always try to do joins this way, makes much cleaner code

http://damonparker.org/blog/2006/02/...yntax-changes/
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Dec 1st, 2008
0

Re: Converting queries from MySQL 4 to MySQL 5

Cheers ... I had a quick glance at this and the scarey thing is, for the most part, I understood it. Not perfectly clear but certainly not overly cloudy either.

Dave
Reputation Points: 10
Solved Threads: 1
Junior Poster
filch is offline Offline
132 posts
since Nov 2008
Dec 1st, 2008
0

Re: Converting queries from MySQL 4 to MySQL 5

lol easiest way to explain it is be specific

MySQL Syntax (Toggle Plain Text)
  1. FROM table1, table2 JOIN table3 on id = table3.id

who knows what that means

now this
MySQL Syntax (Toggle Plain Text)
  1. FROM table1 INNER JOIN table2 on table1.id = table2.id
  2. INNER JOIN table 3 on table3.id = table2.id

looks much clearer at least to me
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: query to find closest
Next Thread in MySQL Forum Timeline: suggest query





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC