0

I think what I need to do can be done using one query, but I'm really not sure - and I'd like to avoid performing a query and then sorting the resultant array if possible.

Basically, I have one table, which includes the following columns:

product_name, price, sold

From these columns, I'd like to do the following:

  • Select the highest 20 values from the 'sold' column DESC;
  • Order the 20 results by price ASC.

Sounds so simple, but can't figure out how to accomplish this to save my life, and SQL is not my strong point. If anyone could help out, it would be appreciated!

4

2 回答 2

4

您可以为此使用子查询:

select t.*
from (select t.*
      from t
      order by sold desc
      limit 20
     ) t
order by price asc

你有一个查询可以做很多事情。我会打电话给这个。这是你要做的:

select t.*
from (select t.*
      from (<subquery
           ) t
      order by sold desc
      limit 20
     ) t
order by price asc
于 2013-02-01T22:27:34.217 回答
0

我认为这将满足您的需求:

select * from table
order by sold desc, price asc
于 2015-12-14T06:34:15.960 回答