【问题标题】:PHP Order by variable after str_padPHP 按 str_pad 后的变量排序
【发布时间】:2013-06-04 19:41:54
【问题描述】:

我想按我从 sql 数据本身创建的变量对我的表进行排序。所以这里是代码

$sql="select * from schedule";
$query=mysql_query($sql);
while($data=mysql_fetch_array($query)){
$new_date=str_pad($data['date'], 10, '0', STR_PAD_LEFT);

$sql="select * from schedule order by $new_date" 有可能吗?

我已经尝试过该代码,但没有成功。所以如果你回答我的问题,我将非常感激,对不起我的英语不好。

【问题讨论】:

  • 你不能只使用 where 子句吗? $sql="select * from schedule where date='$new_date'"

标签: php string date


【解决方案1】:

"select * from schedule order by variable" 注意变量是 schedule 中的一列

【讨论】:

    【解决方案2】:

    这并没有显示您要完成的工作。对您的表进行排序应该在查询$sql 中进行,例如...SELECT * FROM schedule ORDER BY colName 将是一种按 colName 排序的方法。

    【讨论】:

      【解决方案3】:

      您可以使用LPAD(str, len, padstr) 直接填充您的查询:

      ORDER BY LPAD(column, 10, 0)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-09
        • 1970-01-01
        • 1970-01-01
        • 2018-01-11
        • 1970-01-01
        相关资源
        最近更新 更多