需要查询逻辑建议

时间:2011-03-18 16:35:52

标签: mysql

我有一个从数据库中提取的mysql查询,该数据库包含我网站上一小组注册用户使用的简单网络表单的结果。只有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
";

3 个答案:

答案 0 :(得分:0)

尝试:

SELECT
    udb.name
  , data
  , recordtime
FROM userform_db udb
JOIN (select name,max(recordtime) as rtime from userform_db group by name) tmp on udb.name=tmp.name and udb.recordtime=tmp.rtime

答案 1 :(得分:0)

这个SQL应该可以工作:

SELECT m.name,f.`data`,m.recordtime
FROM
 {$conf->website_db}.userform_db f,
 (SELECT name,max(recordtime) mrt 
  FROM {$conf->website_db}.userform_db 
  GROUP BY name) m
WHERE
 f.name=m.name AND f.recordtime=m.mrt
ORDER BY
 m.name

哪个接近你的8.03。缺少首次输入问题听起来更像是数据一致性问题......如果你运行:

 SELECT name,max(recordtime) mrt 
 FROM {$conf->website_db}.userform_db 
 GROUP BY name

本身,您是否获得了每一行的名称和最大值?

答案 2 :(得分:0)

SELECT  uf.*
FROM    (
        SELECT  DISTINCT name
        FROM    userform_db
        ) ufd
JOIN    userform_db uf
ON      uf.id =
        (
        SELECT  id
        FROM    userform_db ufi
        WHERE   ufi.name = ufd.name
        ORDER BY
                ufi.name DESC, ufi.recordtime DESC, ufi.id DESC
        LIMIT 1
        )
ORDER BY
        uf.recordtime DESC