mooomo — 2010-01-18T10:23:06-05:00 — #1
I don't know much about Mysql and can't figure how I would achieve the following. I want to select the 50 most expensive products, and I want to order them alphabetically. The following orders them by prize, but I don't know how to grab those results and order them by something else (alphabetically).
SELECT * FROM `products` ORDER BY `prize` DESC LIMIT 50;
sjh — 2010-01-18T10:30:45-05:00 — #2
Try this. Would test it myself but I'm at work and we don't use MySQL here
FROM ( SELECT *
BY price DESC
LIMIT 50 )
mooomo — 2010-01-18T10:33:36-05:00 — #3
And, for what I've read so far about mysql this is a subquery, right? I thought subqueries were only supposed to return one result. I'm confused about that.
Forgive my ignorance
guido2004 — 2010-01-18T11:05:11-05:00 — #4
No, subqueries return a result set like any other query. This set can be empty, or contain one or more rows.
Why did you think that?
r937 — 2010-01-18T11:27:09-05:00 — #5
actually, a subquery in the SELECT clause may return only one row consisting of one column -- i.e. a singular scalar value, which is why it's called a scalar subquery
elsewhere (e.g. in the FROM clause, or in an IN list or EXISTS predicate) a subquery can return multiple rows
mooomo — 2010-01-18T13:49:39-05:00 — #6
Thanks a lot for the help. That's what was getting me confused. Subquries can really get quite complicated.
If someone knows of some good resources to learn about subqueries, I'll greatly appreaciate if they could link to them. I've already read the official mysql documentation.