Apply ORDER BY after request and LIMIT

Found similar entries, but still stuck - I'm trying to apply sorting AFTER I have processed the query and limited the results. My code

select DISTINCT(t.id) t_id, t.cart_id ,tS.id tS_id, tS.created tS_created, t.value, t.transactionType_id tT_id, tS.member_name, outIn, tT.type type

                            from(transaction t)
                            join transactionSummary tS ON tS.id = t.transactionSummary_id
                            left join transactionType tT ON tT.id = t.transactionType_id
                            order by t.id DESC
                            limit 50

I tried to make an additional choice and apply ORDER BY later, but get an error An unknown column t.id in the list of fields.

The above code (i.e., without selecting sub) works fine, but ORDER BY slows it down since the table is huge ... Any suggestions?

+4
source share
1 answer

Since you are using an alias t.idto t_id, you need to use an alias in an external query.

SELECT *
FROM (select DISTINCT t.id t_id, t.cart_id ,tS.id tS_id, tS.created tS_created, t.value, t.transactionType_id tT_id, tS.member_name, outIn, tT.type type

    from transaction t
    join transactionSummary tS ON tS.id = t.transactionSummary_id
    left join transactionType tT ON tT.id = t.transactionType_id
    limit 50) x
ORDER BY t_id DESC

, , DISTINCT(t.id), , , . DISTINCT SELECT; , , GROUP BY .

, :

select DISTINCT t.id t_id, t.cart_id ,tS.id tS_id, tS.created tS_created, t.value, t.transactionType_id tT_id, tS.member_name, outIn, tT.type type
from transaction t
join (select max(id)-500 maxid from transaction) mT on t.id > maxid
join transactionSummary tS ON tS.id = t.transactionSummary_id
left join transactionType tT ON tT.id = t.transactionType_id
order by t_id DESC
limit 50

500 , .

+3

All Articles