Установить переменную во время запроса - PullRequest
0 голосов
/ 20 февраля 2019

У меня есть следующий запрос

SELECT 
Now() + INTERVAL FLOOR(RAND()*(1000-5+1)+5) DAY as 'Due Date',
device_serial as "Device Serial",
customer_name as "Customer",
error_code as 'Error Code',
resolutiom as 'Resolution'
FROM preventive_maintenance
ORDER BY `timestamp` desc
LIMIT 25

Я хочу заказать в срок, который был найден, как я могу это сделать?

Ответы [ 2 ]

0 голосов
/ 20 февраля 2019
Order By `Due Date` 

должен работать.Вы пробовали это?Если это не так, вы можете использовать подзапрос.

SELECT * FROM 
(
    SELECT 
      Now() + INTERVAL FLOOR(RAND()*(1000-5+1)+5) DAY as 'Due Date',
      device_serial as "Device Serial",
      customer_name as "Customer",
      error_code as 'Error Code',
      resolutiom as 'Resolution'
    FROM preventive_maintenance
) T
ORDER BY `Due Date`
LIMIT 25

Демонстрация

SELECT * from (
SELECT  Now() + INTERVAL FLOOR(RAND()*(1000-5+1)+5) DAY as 'Due Date' from dual
union all 
SELECT  Now() + INTERVAL FLOOR(RAND()*(1000-5+1)+5) DAY as 'Due Date' from dual
union all 
SELECT  Now() + INTERVAL FLOOR(RAND()*(1000-5+1)+5) DAY as 'Due Date' from dual
union all 
SELECT  Now() + INTERVAL FLOOR(RAND()*(1000-5+1)+5) DAY as 'Due Date' from dual
union all 
SELECT  Now() + INTERVAL FLOOR(RAND()*(1000-5+1)+5) DAY as 'Due Date' from dual
union all 
SELECT  Now() + INTERVAL FLOOR(RAND()*(1000-5+1)+5) DAY as 'Due Date' from dual
union all 
SELECT  Now() + INTERVAL FLOOR(RAND()*(1000-5+1)+5) DAY as 'Due Date' from dual
union all 
SELECT  Now() + INTERVAL FLOOR(RAND()*(1000-5+1)+5) DAY as 'Due Date' from dual
union all 
SELECT  Now() + INTERVAL FLOOR(RAND()*(1000-5+1)+5) DAY as 'Due Date' from dual
union all 
SELECT  Now() + INTERVAL FLOOR(RAND()*(1000-5+1)+5) DAY as 'Due Date' from dual
union all 
SELECT  Now() + INTERVAL FLOOR(RAND()*(1000-5+1)+5) DAY as 'Due Date' from dual
union all 
SELECT  Now() + INTERVAL FLOOR(RAND()*(1000-5+1)+5) DAY as 'Due Date' from dual
) T
order by `Due Date`
LIMIT 5
| Due Date            |
| :------------------ |
| 2019-10-27 13:16:15 |
| 2019-11-26 13:16:15 |
| 2020-05-05 13:16:15 |
| 2020-07-01 13:16:15 |
| 2020-07-01 13:16:15 |

дБ <> Fiddle здесь

0 голосов
/ 20 февраля 2019

Просто создайте текущий запрос как подзапрос.

SELECT 
    T.*
FROM
    (SELECT 
        Now() + INTERVAL FLOOR(RAND()*(1000-5+1)+5) DAY as `Due Date`,
        device_serial as `Device Serial`,
        customer_name as `Customer`,
        error_code as `Error Code`,
        resolutiom as `Resolution`
    FROM preventive_maintenance
        -- ORDER BY `timestamp` desc
    LIMIT 25) AS T
ORDER BY T.`Due Date`
...