group by - MySQL order by number of substring occurrences -



group by - MySQL order by number of substring occurrences -

example table:

id name 1 apple: color: yellowish 2 apple: color: reddish 3 grapes: color: greenish 4 grapes: color: greenish 5 oranges: color: orange 6 apple: color: yellowish 7 apple: color: yellowish

i need order number of fruits descending, without looking @ colors. know table have been divided on fruits , colors, thats story.

result should this, ordered count of substring, "everything before first :"

id name 1 apple: color: yellowish 2 apple: color: reddish 6 apple: color: yellowish 7 apple: color: yellowish 3 grapes: color: greenish 4 grapes: color: greenish 5 oranges: color: orange

is possible in mysql, or need sorting in php f.ex?

mysql> select e.* illustration e bring together ( select substring_index(name, ':', 1) fruit, count(*) count illustration grouping fruit) x on substring_index(e.name, ':', 1) = x.fruit order x.count desc, e.id; +----+------------------------+ | id | name | +----+------------------------+ | 1 | apple: color: yellowish | | 2 | apple: color: reddish | | 6 | apple: color: yellowish | | 7 | apple: color: yellowish | | 3 | grapes: color: greenish | | 4 | grapes: color: greenish | | 5 | oranges: color: orange | +----+------------------------+

mysql group-by sql-order-by substring

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -