Wrong Query MySQL selecting the record with oldest or newest Date -
i need know how obtain code of access (idaccess) older (or more recent) date of access user (identified iduser) made table accesstbl using different queries. each user.
create table accesstbl ( idacess varchar(16) unique not null, iduser varchar(16) not null, thedate date not null ); insert accesstbl (idacess, iduser, thedate) values ('a00', '0', '1983-12-30'), ('a01', '0', '2004-09-09'), ('a02', '1', '2013-02-01'), ('a03', '1', '2012-05-09'), ('a04', '2', '1983-12-30'), ('a13', '2', '2013-03-01'), ('a05', '2', '2004-09-09'), ('a06', '3', '2013-02-01'), ('a07', '3', '2012-05-09'), ('a08', '4', '1983-12-30'), ('a09', '4', '2004-09-09'), ('a10', '5', '2013-04-01'), ('a12', '5', '2013-03-01'), ('a11', '5', '2012-05-01');
example:
the user 5 recent 'a10' , oldest 'a11' user 4 recent 'a09' , oldest 'a08'
but, need newest date of users in 1 query.
i tried with...
select idacess, iduser, thedate accesstbl ( thedate in (select max(thedate) accesstbl group iduser) ) group iduser;
but have:
+---------+--------+ | idacess | iduser | +---------+--------+ | a01 | 0 | | a02 | 1 | | a05 | 2 | //bad!!!!! must a13 | a06 | 3 | | a09 | 4 | | a10 | 5 | +---------+--------+ 6 rows in set (0.13 sec)
later, tried with:
select idacess, iduser, max(thedate) accesstbl group iduser;
having...
+---------+--------+--------------+ | idacess | iduser | max(thedate) | +---------+--------+--------------+ | a00 | 0 | 2004-09-09 | //must a01 | a02 | 1 | 2013-02-01 | | a04 | 2 | 2013-03-01 | //must a13 | a06 | 3 | 2013-02-01 | | a08 | 4 | 2004-09-09 | //must a09 | a10 | 5 | 2013-04-01 | +---------+--------+--------------+ 6 rows in set (0.00 sec)
thank valuable help.
ann
if want both oldest , newest simultaneously, you'll need use subqueries compare existing dates (filtered iduser) selected min , max date.
select idacess accesstbl iduser = ? , ( thedate in (select max(thedate) accesstbl iduser = ?) or thedate in (select min(thedate) accesstbl iduser = ?) )
Comments
Post a Comment