1

I am trying to order results from a select statement by the number of commas existing in one of the column.

is there a way to exclusively order by commas in mysql.

4

2 回答 2

2

You can order your results by the number of comma's in a specific field with a query like the following:

select * from table order by length(areaCodeField)-length(replace(areaCodeField, ",", ""))) desc

The order by piece of the query will count all of the characters in the field and then subtract all of the characters not counting the commas. This leaves you with the number of commas.

于 2012-10-12T01:21:59.360 回答
0

You could use a simple workaround (that may be not very efficient):

ORDER BY
    LENGTH(column) - LENGTH(REPLACE(column, ',', '')) asc

I don't know whether other solutions exist, sorry.

于 2012-10-12T01:26:11.287 回答