获取订单升序的最后10行

时间:2013-01-28 23:16:46

标签: php mysql sql mysqli

我已尝试使用标准选择查询进行所有现有尝试,方法是嵌套在另一个SELECT中,然后使用ASC,但在我的JSCharts中由于某种原因无法加载。我的代码工作正常,减去顺序:

SELECT `$tableName`.`$patient_idField` , `$tableDataName`.`$patient_idField` , `$tableDataName`.`$resultField` , `$tableName`.`$login_idField`
FROM `$tableName`
JOIN `$tableDataName` ON `$tableName`.`$patient_idField` = `$tableDataName`.`$patient_idField`
COLLATE utf8_general_ci
WHERE `$tableName`.`$login_idField` = $usssName
ORDER BY `$tableDataName`.id DESC LIMIT 10

以下是尝试重新订购,无法加载:

SELECT * FROM (
SELECT `$tableName`.`$patient_idField` , `$tableDataName`.`$patient_idField` , `$tableDataName`.`$resultField` , `$tableName`.`$login_idField`
FROM `$tableName`
JOIN `$tableDataName` ON `$tableName`.`$patient_idField` = `$tableDataName`.`$patient_idField`
COLLATE utf8_general_ci
WHERE `$tableName`.`$login_idField` = $usssName
ORDER BY `$tableDataName`.id DESC LIMIT 10 ) AS `$tableName` JOIN `$tableDataName` ORDER by `$tableDataName`.id ASC

没有PHP更新......

SELECT userlist.patient_id, results.patient_id, results.result, userlist.login_id
FROM userlist
JOIN results ON userlist.patient_id = results.patient_id
COLLATE utf8_general_ci
WHERE userlist.login_id = ####
ORDER BY results.id DESC
LIMIT 10 

现在,如果我在ASC之外执行任何操作,则会在patient_id重复时返回错误。

有效的代码,供将来参考

SELECT  *
FROM (
SELECT userlist.patient_id, results.result, userlist.login_id, results.id
FROM userlist
  JOIN results ON userlist.patient_id = results.patient_id
COLLATE utf8_general_ci
WHERE userlist.login_id = ####
ORDER BY results.id DESC
LIMIT 10 ) temp
ORDER BY id

它涉及删除SELECT上的.patient_id之一。

2 个答案:

答案 0 :(得分:2)

嵌套在另一个选择中应该有效;怎么会失败?

类似的东西:

select tablenamepatientidfield, tabledatanamepatientidfield, resultfield, loginidfield from (
    SELECT
        `$tableDataName`.id,
        `$tableName`.`$patient_idField` AS tablenamepatientidfield,
        `$tableDataName`.`$patient_idField` AS tabledatanamepatientidfield,
        `$tableDataName`.`$resultField` AS resultfield,
        `$tableName`.`$login_idField` AS loginidfield,
    FROM `$tableName`
    JOIN `$tableDataName` ON `$tableName`.`$patient_idField` = `$tableDataName`.`$patient_idField`
    COLLATE utf8_general_ci
    WHERE `$tableName`.`$login_idField` = $usssName
    ORDER BY `$tableDataName`.id DESC LIMIT 10
) AS descending_select ORDER BY id;

(有点尴尬的是,列名都是变量并且可能会发生冲突(并且不包括id本身)。)

答案 1 :(得分:1)

你能做到吗

select * from (
    SELECT `$tableName`.`$patient_idField` 
         , `$tableDataName`.`$patient_idField` 
         , `$tableDataName`.`$resultField` 
         , `$tableName`.`$login_idField`
         ,`$tableDataName`.id
    FROM `$tableName`
      JOIN `$tableDataName` ON `$tableName`.`$patient_idField` 
                             = `$tableDataName`.`$patient_idField`
    COLLATE utf8_general_ci
    WHERE `$tableName`.`$login_idField` = $usssName
    ORDER BY `$tableDataName`.id DESC LIMIT 10 ) t
order by id

试试这个,我编辑了以更好地反映你实际在做什么

select  *
from (
    SELECT userlist.patient_id, results.result, userlist.login_id
    FROM userlist
      JOIN results ON userlist.patient_id = results.patient_id
    COLLATE utf8_general_ci
    WHERE userlist.login_id = ####
    ORDER BY results.id DESC
    LIMIT 10 ) temp
order by patient_id