Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在SQL中使用ORDERBY子句_Mysql_Sql - Fatal编程技术网

Mysql 在SQL中使用ORDERBY子句

Mysql 在SQL中使用ORDERBY子句,mysql,sql,Mysql,Sql,是否可以使用关键字按结果在数据表中出现的次数对SELECT查询中的结果进行排序 MySQL: SELECT * FROM interests ORDER BY (keyword) 表中的值为: hiking running swimming hiking photography swimming hiking 如果返回顺序基于表中每个值出现的频率,则应为: hiking hiking hiking swimming swimming running photography 如果没有一个

是否可以使用关键字按结果在数据表中出现的次数对SELECT查询中的结果进行排序

MySQL:

  SELECT * FROM interests ORDER BY (keyword)
表中的值为:

hiking
running
swimming
hiking
photography
swimming
hiking
如果返回顺序基于表中每个值出现的频率,则应为:

hiking
hiking
hiking
swimming
swimming
running
photography
如果没有一个特定的关键字,如何使用SQL实现这一点

谢谢

将按升序给出每个关键字的计数。但是你不会有3次徒步旅行,2次游泳等等。。。只是远足,3次,游泳,2次等等

SELECT  a.*
FROM    interests a
        INNER JOIN 
        (
            SELECT  keyword, COUNT(*) totalCount
            FROm    interests
            GROUP BY keyword
        ) b ON a.keyword = b.keyword
ORDER   BY b.totalCount DESC

尝试分组方式和计数(*)。我不知道您的表架构,因此如果您显示它会有所帮助。请使用
order by cnt desc
首先获取最高计数。如果我要使用SELECT*,该怎么办?您可以选择任意数量的字段。如果您提取未分组/聚合的字段,则结果可能有点未定义。这太棒了,谢谢!现在,我如何让我的SQL只显示每个值中的一个?我怎样才能得到每个值的计数?每个值的数字是什么意思?这不是@MarcB的答案吗?
SELECT  a.*
FROM    interests a
        INNER JOIN 
        (
            SELECT  keyword, COUNT(*) totalCount
            FROm    interests
            GROUP BY keyword
        ) b ON a.keyword = b.keyword
ORDER   BY b.totalCount DESC
    SELECT 
    t1.*,count(t1.keyword) 
    FROM 
    interests AS t1 INNER JOIN (
     SELECT 
            keyword,count(*) totalcount 
      FROM 
            interests GROUP BY keyword
      )t2 
ON t1.keyword=t2.keyword group by t1.keyword 
   ORDER BY t2.totalcount DESC