我有一個MySQL查詢從數據庫中提取,該數據庫包含我的站點上一小組註冊用戶使用的簡單Web表單的結果。只有3個字段被拉出,並通過「姓名」(用戶名),「數據」(其他用戶輸入數據)和「錄製時間」(提交時間/日期)在查詢中表示。目標是在結果網頁表中僅顯示7個用戶中每個用戶的最新條目。我有3個查詢版本在不同程度上工作,但沒有完全正確。出於某種原因,所有3都省略了第一個(按字母順序排列的用戶名)條目。無需根據預期對列表進行排序以在表格的頂部顯示最新的用戶輸入(按日期/時間)(按日期/時間從那裏下降)。示例#1效果最好,但按用戶名按字母順序排序。 #2和#3的行爲非常相似,但在最上面顯示最新的用戶條目(所有用戶),然後按字母順序排列剩餘的用戶名。我可以讓所有合法用戶條目顯示的唯一方法是創建了假用戶「1A」並提交了一個條目。只要它佔據了第一個(按字母順序排列的用戶名)條目,剩餘的條目就會顯示在顯示網格上。如果您對其他查詢邏輯策略有任何想法,我會很感激您的想法。謝謝,邁克需要查詢邏輯建議
1. This version 8.01 returns all user entries except the first alphabetical username and sorts by username.
$myquery = "
SELECT
name
, data
, recordtime
FROM
(
SELECT
*
FROM
{$conf->website_db}.userform_db
ORDER BY
recordtime DESC
) AS x
GROUP BY
name
";
2. This version 8.02 returns all user entries except the first alphabetical username and sorts by username,
except places the most recent entry (of all users) at the top of the list.
$myquery = "
SELECT
m1.*
FROM
{$conf->website_db}.userform_db m1
LEFT JOIN
{$conf->website_db}.userform_db m2
ON (m1.name = m2.name AND m1.recordtime website_db}.userform_db a,
(SELECT name, MAX(recordtime) AS Date
FROM {$conf->website_db}.userform_db
GROUP BY name) b
WHERE a.name = b.name
AND a.recordtime = b.Date
";
3.This version is 8.03 returns all user entries except the first alphabetical username and sorts by username,
except places the most recent entry (of all users) at the top of the list (same as v2).
$myquery = "
SELECT
a.name, a.data, a.recordtime
FROM
{$conf->website_db}.userform_db a,
(SELECT name, MAX(recordtime) AS Date
FROM {$conf->website_db}.userform_db
GROUP BY name) b
WHERE a.name = b.name
AND a.recordtime = b.Date
";
感謝您的意見。此查詢導致錯誤「字段列表中的列名稱」不明確「 – user641472 2011-03-18 19:16:09
yah .. it; s udb.name there – 2011-03-18 19:21:36