dbTalk Databases Forums  

Select all but the latest row

comp.databases.mysql comp.databases.mysql


Discuss Select all but the latest row in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Mark Smith
 
Posts: n/a

Default Select all but the latest row - 11-17-2010 , 04:13 AM






Hi,

I have a table to store the login history of clients. One of the
captured fields is the timestamp. I am trying to write a query to get
all but the last row in the login history for a user.

I thought something like this should work:

SELECT * FROM LoginHistory L
WHERE USER_ID like 'testuser (AT) company (DOT) com'
HAVING L.TIMESTAMP < MAX(L.TIMESTAMP);

This seems to select only the first row. Any idea why this is
happening and how to do it correctly?

Thanks

Reply With Quote
  #2  
Old   
Luuk
 
Posts: n/a

Default Re: Select all but the latest row - 11-17-2010 , 04:27 AM






On 17-11-10 11:13, Mark Smith wrote:
Quote:
Hi,

I have a table to store the login history of clients. One of the
captured fields is the timestamp. I am trying to write a query to get
all but the last row in the login history for a user.

I thought something like this should work:

SELECT * FROM LoginHistory L
WHERE USER_ID like 'testuser (AT) company (DOT) com'
HAVING L.TIMESTAMP< MAX(L.TIMESTAMP);

This seems to select only the first row. Any idea why this is
happening and how to do it correctly?

Thanks
SELECT * FROM LoginHistory L
WHERE USER_ID like 'testuser (AT) company (DOT) com'
HAVING L.TIMESTAMP< (
SELECT MAX(M.TIMESTAMP)
FROM LoginHistory M
WHERE M.USER_ID like 'testuser (AT) company (DOT) com');


--
Luuk

Reply With Quote
  #3  
Old   
Mark Smith
 
Posts: n/a

Default Re: Select all but the latest row - 11-17-2010 , 06:21 AM



On Nov 17, 10:27*am, Luuk <L... (AT) invalid (DOT) lan> wrote:
Quote:
On 17-11-10 11:13, Mark Smith wrote:



Hi,

I have a table to store the login history of clients. One of the
captured fields is the timestamp. I am trying to write a query to get
all but the last row in the login history for a user.

I thought something like this should work:

SELECT * FROM LoginHistory L
WHERE USER_ID like 'testu... (AT) company (DOT) com'
HAVING L.TIMESTAMP< *MAX(L.TIMESTAMP);

This seems to select only the first row. Any idea why this is
happening and how to do it correctly?

Thanks

SELECT * FROM LoginHistory L
WHERE USER_ID like 'testu... (AT) company (DOT) com'
HAVING L.TIMESTAMP< *(
* * * * SELECT MAX(M.TIMESTAMP)
* * * * FROM LoginHistory M
* * * * WHERE M.USER_ID like 'testu... (AT) company (DOT) com');

--
Luuk
Thanks.

Although is there a reason mysql can't / won't do this with just one
select?

Reply With Quote
  #4  
Old   
Axel Schwenke
 
Posts: n/a

Default Re: Select all but the latest row - 11-17-2010 , 06:55 AM



Mark Smith <marksmith5555 (AT) jungle-monkey (DOT) com> wrote:
Quote:
I have a table to store the login history of clients. One of the
captured fields is the timestamp. I am trying to write a query to get
all but the last row in the login history for a user.

I thought something like this should work:

SELECT * FROM LoginHistory L
WHERE USER_ID like 'testuser (AT) company (DOT) com'
HAVING L.TIMESTAMP < MAX(L.TIMESTAMP);
SELECT * FROM LoginHistory L
WHERE USER_ID like 'testuser (AT) company (DOT) com'
ORDER BY L.TIMESTAMP DESC
LIMIT 1, $REALLY_LARGE_NUMBER

See: http://dev.mysql.com/doc/refman/5.1/en/select.html
for the LIMIT clause

Better omit the LIMIT clause and simply drop the first
tuple from the result set.

Of course this query will have bad performance, especially
if there are many entries for a given user.

Last not least: USER_ID should be better a number than a
string and you should use the = operator instead of LIKE.


XL

Reply With Quote
  #5  
Old   
Luuk
 
Posts: n/a

Default Re: Select all but the latest row - 11-17-2010 , 07:12 AM



On 17-11-10 13:21, Mark Smith wrote:
Quote:
On Nov 17, 10:27 am, Luuk<L... (AT) invalid (DOT) lan> wrote:
On 17-11-10 11:13, Mark Smith wrote:



Hi,

I have a table to store the login history of clients. One of the
captured fields is the timestamp. I am trying to write a query to get
all but the last row in the login history for a user.

I thought something like this should work:

SELECT * FROM LoginHistory L
WHERE USER_ID like 'testu... (AT) company (DOT) com'
HAVING L.TIMESTAMP< MAX(L.TIMESTAMP);

This seems to select only the first row. Any idea why this is
happening and how to do it correctly?

Thanks

SELECT * FROM LoginHistory L
WHERE USER_ID like 'testu... (AT) company (DOT) com'
HAVING L.TIMESTAMP< (
SELECT MAX(M.TIMESTAMP)
FROM LoginHistory M
WHERE M.USER_ID like 'testu... (AT) company (DOT) com');

--
Luuk

Thanks.

Although is there a reason mysql can't / won't do this with just one
select?
its not mysql that can't do it with one select
or can you show /me ?

--
Luuk

Reply With Quote
  #6  
Old   
strawberry
 
Posts: n/a

Default Re: Select all but the latest row - 11-18-2010 , 03:49 AM



For consideration...

SELECT * FROM ints;
+---+
Quote:
i |
+---+
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
+---+

SELECT DISTINCT i1.* FROM ints i1 JOIN ints i2 ON i2.i > i1.i;
+---+
Quote:
i |
+---+
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
+---+

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.