PHP:MY​​SQL QUERY Ordery By

时间:2016-12-26 21:29:17

标签: php mysql mysqli

图像表 enter image description here

你好,我有一张表$sql_query = $connection->query("SELECT * FROM comments WHERE `post_id` = '38' ORDER BY `id` DESC LIMIT 3"); while ($ff = $sql_query->fetch_array(MYSQLI_ASSOC)) { echo $ff["text"]. "</br>"; } ,我需要采取最后三条评论,但另一种方式是:

<connectionStrings>
    <add name="EquipamentoDal.Properties.Settings.ExtinguisherMngConnectionString"
         connectionString="Data Source=192.168.1.66,49170;Initial Catalog=ExtinguisherMng;Integrated Security=True"
         providerName="System.Data.SqlClient" />
 </connectionStrings>

我的代码显示了这个:

DESKTOP-HU51203\EXPRESS

代码:

def common_names(year, filename, N):
    yearcol = str(year)
    namecol = 'JMÉNO'
    df = (
        pandas.read_csv(filename)
            .sort_values(by=[yearcol], ascending=False)
            .head(N)
    )[[namecol, yearcol]]
    return df

提前致谢!

2 个答案:

答案 0 :(得分:2)

您可以使用子查询执行此操作,如this问题中所述。但是,使用PHP的array_reverse()

可能更容易
$rows = array_reverse($sql_query->fetch_all(MYSQLI_ASSOC));
foreach ($rows as $ff)
    echo $ff["text"]. "</br>";

使用子查询,如果您愿意:

SELECT * FROM (
  SELECT * FROM comments
  WHERE `post_id` = '38'
  ORDER BY `id` DESC LIMIT 3
) t ORDER BY t.id

基本上,这会使用原始查询的结果创建一个别名为t的派生表,并重新排序此有限的结果集。

答案 1 :(得分:0)

以下查询将提供您的desider输出

select * from (SELECT * FROM comments WHERE `post_id` = '38' ORDER BY `id` DESC LIMIT 3) temp order by id