php如何从tabel获取主题标签

时间:2016-07-20 13:22:44

标签: php

我想从这个tabel(sql)中获取主题标签,但按大多数使用排序 - 少用

enter image description here

示例:

#gg (9 times i want to select first beacouse is most used)
#gogogo (4 times)
#simple_1 (3 times)
#simple_2 (1 times)

如何使用php命令获取它:

$sql_query = mysqli_query($connection, 'SELECT FROM hashtags... ?')

2 个答案:

答案 0 :(得分:2)

需要

ORDER BY 关键字才能按used列对表格进行排序。像这样,

SELECT hashtag,used FROM hashtags ORDER BY used;

以上查询将按升序排序(少用)

对结果进行排序

ORDER BY 默认按升序对结果集进行排序,按降序对其进行排序更新此类查询,为此我们使用 DESC 关键字列名。

SELECT hashtag,used FROM hashtags ORDER BY used DESC;

以上查询将按降序排列结果(最常用)

参考:http://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html

  

ORDER BY关键字用于按一列或多列对结果集进行排序。

答案 1 :(得分:0)

使用PDO:

<?php

    //DATABASE CONNECTION CONFIGURATION:
    defined("HOST")   or define("HOST",   "localhost");   //<== YOUR HOST
    defined("DBASE")  or define("DBASE",  "yourDB");      //<== YOUR DATABASE
    defined("USER")   or define("USER",   "root");        //<== YOUR USERNAME
    defined("PASS")   or define("PASS",   "root");        //<== YOUR PASS

    try {
        $dbh        = new \PDO('mysql:host='.HOST.';dbname='. DBASE,USER,PASS);
        $dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

        // YOUR QUERY......
        $query      = " SELECT `used`, `hashtag` FROM `hashtags` ORDER BY `used` DESC ";
        $stmt       = $dbh->prepare($query);
        $resultSet  = $stmt->fetchObject();

        var_dump($resultSet);


    }catch(PDOException $e){
        throw new Exception($e->getMessage());
    }