If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > using subquery in mysql (was "Help get this query working")

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-15-05, 06:02
spib spib is offline
Registered User
 
Join Date: Feb 2005
Posts: 1
using subquery in mysql (was "Help get this query working")

Hi,

I have a table called 'myTable' which consists of two fields, field_id (int) and time (DateTime). I have a query which works fine in SqlServer which won't work in MySql.

Code:
select *, (select count(*) + 1 from myTable t where t.time < m.time) as Rank from myTable m order by Rank desc
The query is supposed to display a list of times with a ranking. When I run the above query I get an error

Code:
#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 'select count(*) + 1 from myTable t whe
Can anyone help me?

Cheers
Reply With Quote
  #2 (permalink)  
Old 02-15-05, 09:31
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
you cannot use subqueries in mysql unless you are on at least version4.1
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-15-05, 09:35
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
to use the following solution, you cannot use "select star"

you must itemize all the columns in both the SELECT list and the GROUP BY
Code:
select t.foo
     , t.bar
     , t.time
     , count(*) + 1 as Rank
  from myTable as t
left outer
  join myTable as m  
    on t.time < m.time
group
    by t.foo
     , t.bar
     , t.time
order 
    by Rank desc
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On