简体   繁体   中英

how to sort “GROUP BY” query result by Frequent values?

i write php application that it get name of the user browsers:

+----------+------------+
| name     | browser    |
+----------+------------+
| Puffball | firefox    |
| Chirpy   | edge       |
| Whistler | chrome     |
| Slim     | firefox    |
| Claws    | edge       |
| Fluffy   | chrome     |
| Fang     | chrome     |
| Bowser   | safari     |
| Buffy    | chrome     |
+----------+------------+

i use this sql query to get browsers with "GROUP BY" and the result is:

$sql = "SELECT * FROM views WHERE linkid = '$string' GROUP BY browser";

array (size=4)
  0 => string 'firefox'
  1 => string 'edge'
  2 => string 'chrome'
  3 => string 'safari'

i want to sort this result by Frequent value, like this:

array (size=4)
  0 => string 'chrome'
  1 => string 'edge'
  2 => string 'firefox'
  3 => string 'safari'

tanx!

In order to get the result ordered by frequency that the browser is mentioned in the input table you need to add a count like this, and then ORDER BY the count column

SELECT *, count(browser) as frequency
FROM views 
WHERE linkid = '$string' 
GROUP BY browser
ORDER by frequency

Use this below Sql Query

SELECT * FROM `table` group by browser order by browser asc

Use PHP array Sort

sort($browser);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM