P
Prisma•3mo ago
zenakent

is it possible to order a column how I want it?

For example, I have a table with a column that has values A,B,C,D when I query that table, I want to sort the results by D,B,A,C
2 Replies
zenakent
zenakentOP•3mo ago
found the answer. I have to use prisma raw query and have to use order by "case" to have the results sorted the way I want it. so something like ORDER BY CASE WHEN at.status = 'D' THEN 1 WHEN at.status = 'B' THEN 2 ELSE 3 END,
RaphaelEtim
RaphaelEtim•3mo ago
Hi @zenakent 👋 Prisma doesn't support custom sorting orders directly in its query syntax. You can use raw SQL with a CASE statement. For example:
const results = await prisma.$queryRaw`
SELECT *
FROM yourTable
ORDER BY CASE yourColumn
WHEN 'D' THEN 1
WHEN 'B' THEN 2
WHEN 'A' THEN 3
WHEN 'C' THEN 4
ELSE 5
END;
`;
const results = await prisma.$queryRaw`
SELECT *
FROM yourTable
ORDER BY CASE yourColumn
WHEN 'D' THEN 1
WHEN 'B' THEN 2
WHEN 'A' THEN 3
WHEN 'C' THEN 4
ELSE 5
END;
`;
Want results from more Discord servers?
Add your server