Query logic suggestions needed
I have a mysql query pulling from a db that holds results from a simple web-form used by a small group of registered users at my site. Only 3 fields are pulled and are represented in the query by “name”(username), “data”(misc. user input data), and “recordtime”(the time/date of submission). The goal is to display only the most recent entry for each of 7 users on the resulting webpage table. I have 3 query versions that work to varying degrees, but none exactly right. For some reason all 3 omit the first (alphabetical by username) entry. None sort the list to display the latest user entry (by date/time) on the top of the table (descending down from there by date/time) as intended. Example #1 works the best, but sorts alphabetically by username. #2 and #3 behave very similarly, but display the latest user entry (of all users) on top, then sort the remainder by username alphabetically. The only way I can get all legitimate user entries to display is that I’ve created a fake user “1A” and submitted an entry. As long as it’s there occupying the first (alphabetical username) entry, the remaining entries will show on the display grid. If you have any ideas on other query logic strategies, I would appreciate your thoughts. Thanks, Mike
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,
开发者_如何学C (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
";
This SQL should work:
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
Which is close to your 8.03. The lacking first entry problem sounds more like a data consistency problem... if you run:
SELECT name,max(recordtime) mrt
FROM {$conf->website_db}.userform_db
GROUP BY name
By itself, do you get a name and max value for every row?
Try:
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
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
精彩评论