LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-07-2008, 02:54 PM   #1
rtaft
Member
 
Registered: Aug 2003
Posts: 85

Rep: Reputation: 15
MySQL query question


Let's say I have a table that has the time and my name in it. Every minute an entry is added to the DB with the current time, and my name. Every so often I change my name. Using this setup (not the actual one but close to what I am dealing with), how can I select the times when my name changed? There are other attributes that change, which is why it is input frequently, but I just want to know when the name changes.

I can use:
SELECT MIN(time), name FROM table GROUP BY name;

Which works if I never use the same name twice. I'm looking for something that might work for when the name changes back to something it has been before.
IE
TIME | NAME
1 | Jim
2 | Jim
3 | Fred
4 | Fred
5 | Fred
6 | Jim
7 | Alex
8 | Joe
9 | Joe
....

Where this would result in
TIME | NAME
1 | Jim
3 | Fred
6 | Jim
7 | Alex
8 | Joe

With my query above, '6 | Jim' is missing
 
Old 10-07-2008, 03:13 PM   #2
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
I haven't done SQL in a long time, but if the TIME column always increments by one (or if you have a column that always increments by 1 chronologically), then you could just search for rows where the name is different than the last:
Code:
SELECT * 
FROM table a
WHERE a.TIME != 0 AND a.NAME != (SELECT b.NAME FROM table b WHERE b.TIME = (a.TIME - 1))
DISCLAIMER:
I assume that TIME starts at 0 and increments by 1 for every entry (if it does not, add a column that does this)
I have not tested this
I think this will work but I'm note sure if 'a.TIME - 1' is proper SQL syntax.
 
Old 10-07-2008, 03:26 PM   #3
rtaft
Member
 
Registered: Aug 2003
Posts: 85

Original Poster
Rep: Reputation: 15
Thanks, this actually worked! I had to change the start time, and the increment to 60000, but it works!
 
  


Reply



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

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
interesting MySQL query/view query :s mjh Programming 3 03-25-2008 07:30 AM
mysql use output of one query in another query secretlydead Programming 2 11-19-2007 01:25 AM
help with mysql query: return nth rows in query hawarden Programming 2 07-31-2006 06:36 PM
PHP MySQL Query Question vi0lat0r Programming 1 07-15-2004 05:02 AM
A Simple MySQL Query Question (I have Bad Syntax) bpk General 1 12-02-2003 09:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration