MySQL不按毫秒排序

时间:2014-01-13 08:31:10

标签: php mysql

我面临一个奇怪的问题。

MySQL没有按正确的顺序排序。

我的查询是:

 SELECT a.quiz_id, a.time, b.images, b.prize_title FROM entries a, quiz b WHERE a.quiz_id = b.id AND a.member = 1  ORDER BY a.time ASC  

a.time是毫秒,但是我得到了这个输出: enter image description here

如何按“最快”条目排序? ORDER BY a.time不起作用?

编辑:

这显示正确的列表:

SELECT DISTINCT a.quiz_id, a.time, b.images, b.prize_title FROM entries a, quiz b WHERE a.quiz_id = b.id AND a.member = 1 ORDER BY a.time ASC

但是当我添加“group by a.quiz_id”时,它没有显示正确的条目?

SELECT DISTINCT a.quiz_id, a.time, b.images, b.prize_title FROM entries a, quiz b WHERE a.quiz_id = b.id AND a.member = 1 GROUP BY a.quiz_id ORDER BY a.time ASC

2 个答案:

答案 0 :(得分:1)

如果我看到你的陈述(图片)你没有使用a.time而只是'时间'。 '时间'是一种类型,所以你可以试着把时间放在``=> `time`。有关详细信息,请参阅http://dev.mysql.com/doc/refman/5.6/en/time.html

答案 1 :(得分:0)

使用DECIMAL字段表示时间跨度值的工作示例。

<?php
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
setup($pdo);


$q = '
    SELECT
        a.quiz_id, a.`time`
    FROM
        soFoo as a
    ORDER BY `time` ASC
';
foreach( $pdo->query($q, PDO::FETCH_ASSOC) as $r) {
    echo join(', ', $r), "\r\n";
}


function setup($pdo) {
    $pdo->exec('
        CREATE TEMPORARY TABLE soFoo (
            id int auto_increment,
            quiz_id int,
            `time` Decimal(14,10),
            primary key (id)
        )
    ');

    $stmt = $pdo->prepare('INSERT INTO soFoo (quiz_id, `time`) VALUES (?,?)');
    $stmt->execute( array(3, '1.06445693970') );
    $stmt->execute( array(3, '1.85788297653') );
    $stmt->execute( array(1, '1298.33279896') );
    $stmt->execute( array(1, '24.8014650345') );
    $stmt->execute( array(1, '3.44685196877') );
    $stmt->execute( array(1, '33.5646929741') );
    $stmt->execute( array(1, '6.26836800575') );
    $stmt->execute( array(1, '7.55817890167') );
}

打印

3, 1.0644569397
3, 1.8578829765
1, 3.4468519688
1, 6.2683680058
1, 7.5581789017
1, 24.8014650345
1, 33.5646929741
1, 1298.3327989600