Solved

Subquery doesn't work, syntax is correct???

Posted on 2004-04-03
9
357 Views
Last Modified: 2008-03-17
I just installed Mysql 4.0.18

I tried to run ths sql statement

Select a.price, a.name
from rooms
where a.price = (Select max(price) from rooms);


I don't believe the syntax is wrong.  Can anyone help me?  I checked the data in the [Rooms] table which are all valid.

When I run:
Select max(price) from rooms;

It works, but when its in a subquery it doesn't.

Any help is greatly appreciated
0
Comment
Question by:phake123
  • 3
  • 3
  • 2
  • +1
9 Comments
 

Author Comment

by:phake123
ID: 10750209
I get this error:
Error 1064:  You have an error in your SQL syntax.  Check the manual that correspond to your MYSQL server version for the right syntax to use near 'Select max(price) from rooms' at line 3
0
 
LVL 7

Expert Comment

by:mquiroz
ID: 10750764
unless you use mysql 4.1, mysql doesn't support subqueries...
0
 
LVL 17

Expert Comment

by:Squeebee
ID: 10755076
An alternative is this:

Select @max := max(price) from rooms;

Select a.price, a.name
from rooms
where a.price = @max;

Execute the two in series within the same database connection.
0
 
LVL 1

Accepted Solution

by:
Dossy earned 50 total points
ID: 10756530
What you want is this:

SELECT   a.price, a.name
FROM     rooms AS a
WHERE   a.price IN (
    SELECT   MAX(price)
    FROM     rooms
);

However, from this particular query I have a feeling what you REALLY want is this:

SELECT   price, name
FROM      rooms
HAVING   price = MAX(price);

The first query using the subquery will do two scans of the table -- once for the outer query, and once for the inner subquery.  The second query (with the HAVING clause) should only scan the table once, which should be somewhat faster.

-- Dossy
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 17

Expert Comment

by:Squeebee
ID: 10756818
Dossy: We have already established that the user cannot use subqueries, so the first example os out.  As for the second one: did you try this? You are combining an aggregate function with non-aggregate functions, which will cause MySQL to error out on illegal syntax.
0
 
LVL 1

Expert Comment

by:Dossy
ID: 10757365
Squeebee:  You're right, phake123 did say MySQL 4.0 -- oops.  And yes, the second query doesn't do what I thought.

Creating a simple test:

mysql> create table rooms (price int, name varchar(100));
mysql> insert into rooms values (10, "ten");
mysql> insert into rooms values (11, "eleven");        
mysql> insert into rooms values (15, "fifteen");        
mysql> insert into rooms values (5, "five");

This isn't optimal (as the ORDER BY still causes MySQL to do a full table scan and a filesort) but it works:

SELECT   price, name
FROM     rooms
ORDER   BY price DESC
LIMIT     1;

Returns the "15, fifteen" row.

However, your suggestion of using a variable to store MAX(price) and using that in a subsequent WHERE clause is definitely the most optimal solution since it CAN take advantage of indexes, etc.

0
 
LVL 17

Expert Comment

by:Squeebee
ID: 10757440
Hi Dossy;

Sorry to nit-pick but there is one scenario you need to consider: What happens when more than one product exists at the highest price? In such a situation you will only return one of the highest priced items, but if you had two items that each cost $15 only one would be returned. I'm not criticizing, just giving your something else to think about.
0
 
LVL 1

Expert Comment

by:Dossy
ID: 10759036
Yup, you're right.
0
 

Author Comment

by:phake123
ID: 10760662
Thanks everyone.  All your inputs were great!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now