Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql custom order by and alphabetical order by:

Tags:

php

mysql

I am using the following query:

$query = "SELECT * FROM `$table[$a]` ORDER BY FIELD(typeof,'pdf','swf','img','web')";

to select and customly order my mysql query, it works great except there are multiple files within each typeof and I now want to order them alphabetically yet retaining their typeof order. Make sense?

like image 450
George Reith Avatar asked Aug 11 '11 11:08

George Reith


People also ask

How do I sort by alphabetical order in MySQL?

Use the ORDER BY clause to sort the result set by one or more columns. Use the ASC option to sort the result set in ascending order and the DESC option to sort the result set in descending order.

Can we use 2 ORDER BY in MySQL?

If you want to select records from a table but would like to see them sorted according to two columns, you can do so with ORDER BY . This clause comes at the end of your SQL query.

How do I sort a custom order in SQL?

By default SQL ORDER BY sort, the column in ascending order but when the descending order is needed ORDER BY DESC can be used. In case when we need a custom sort then we need to use a CASE statement where we have to mention the priorities to get the column sorted.

How do you use group by and ORDER BY together?

When combining the Group By and Order By clauses, it is important to bear in mind that, in terms of placement within a SELECT statement: The GROUP BY clause is placed after the WHERE clause. The GROUP BY clause is placed before the ORDER BY clause.


1 Answers

SELECT * FROM `$table[$a]` 
ORDER BY 
  FIELD(typeof,'pdf','swf','img','web'), --first order by type
  filename  --then by filename
like image 63
Mchl Avatar answered Sep 19 '22 02:09

Mchl