U
U
undeadter2019-04-08 03:22:43
MySQL
undeadter, 2019-04-08 03:22:43

How to make a query with sorting in MySQL?

there is a mysql table
contains fields rate and amount
it contains many values ​​rate = 0.01 and different amounts
also contains many different values ​​rate and different amounts
I want to get all amounts for the first 15 values ​​rate = 0.01, 0.03, 0.04 ... 0.16
This command returns 15 amount values ​​for one rate:

"SELECT * FROM `cell` WHERE `buy_cell` = 0 ORDER BY `rate` DESC LIMIT 15"

How to get absolutely all amount values ​​for 15 different rates?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mthps, 2019-04-08
@mthps

Depending on how the result is expected
SELECT * FROM `cell` WHERE `buy_cell` = 0 AND rate BETWEEN 0.01 AND 0.16 ORDER BY `rate` DESC
or
SELECT *, GROUP_CONCAT(amount) FROM `cell`
WHERE `buy_cell` = 0 AND rate BETWEEN 0.01 AND 0.16
GROUP BY rate
ORDER BY `rate` DESC

I
idShura, 2019-04-08
@idShura

If the rate field can contain the same values, then perhaps you need to do this:

SELECT a.`rate`,
       b.`amount`
  FROM (SELECT `rate` 
          FROM `cell` 
         WHERE `buy_cell` = 0 
         GROUP BY `rate` 
         ORDER BY `rate` DESC 
         LIMIT 15
        ) AS a
  LEFT JOIN `cell` AS b on b.`rate` = a.`rate`

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question