Hi!
BACKGROUND
I have a table containing user information on my web server. This is an example:
ID IP DATE USER
---------------------------------------------
1 123.123.123.123 2002/04/24 10:05 Peter
2 234.234.234.234 2002/04/23 09:15
3 321.321.321.321 2002/04/24 11:06 John
4 234.234.234.234 2002/04/10 15:15 Mark
5 234.234.234.234 2002/04/09 16:34 Bill
ID is a counter
IP is the address of the user's computer
DATE is the date/time when the user accessed the web server
USER is the name of the user
As you can see from my example, the field USER can be empty. If the user logs in with his name, USER will be updated accordingly, otherwise it will be empty.
THIS IS WHAT I WANT TO DO
On the webpage I want to present information about who has accessed the site. If the USER field in a specific record in the table is empty, the table should be searched for a previous record with the same IP and with the USER field present.
In other words, if the user has not logged in and USER hence is empty, look for a previous login from the *same* IP and use the USER field from that record.
In the example above, record 2, 4 and 5 has the same IP. The information from record 2 should be supplemented with the USER from record 5. The reason to choose 5 and not 4 is to use the *latest* known user of that IP.
THIS IS WHAT I HAVE TRIED
Idea 1: Create a "lookup table" which only contains associated IPs and USERs. Then I join the main table with the lookup table.
SQL:
SELECT t1.id,t1.date,t1.ip,t1.user,t2.user FROM counter t1 LEFT JOIN (SELECT DISTINCT ip,user FROM counter WHERE user<>'') AS t2 ON t1.ip=t2.ip ORDER BY id DESC
The problem with this approach is the the lookup table will contain *several* records with the same IP but different name. When joining with the master table, all combinations between the "master record" and the lookup entries will appear.
In order to get this approach to work, I need to make sure that the lookup table only contains *one* entry per IP. This entry should be the one with the most recent date.
ID IP DATE T1.USER T2.USER
---------------------------------------------------------
5 234.234.234.234 2002/04/09 16:34 Bill Bill
5 234.234.234.234 2002/04/09 16:34 Bill Mark
4 234.234.234.234 2002/04/10 15:15 Mark Mark
4 234.234.234.234 2002/04/10 15:15 Mark Bill
3 321.321.321.321 2002/04/24 11:06 John John
2 234.234.234.234 2002/04/23 09:15 Mark
2 234.234.234.234 2002/04/23 09:15 Bill
1 123.123.123.123 2002/04/24 10:05 Peter
T1.USER is the user from the master table, T2.USER is the lookup name.
The idea was to present the lookup name in those cases the master name was empty.
Ok, this explaination was perhaps a bit long. I hope somwone will be able to help me out!
Cheers,
Rally2002
BACKGROUND
I have a table containing user information on my web server. This is an example:
ID IP DATE USER
---------------------------------------------
1 123.123.123.123 2002/04/24 10:05 Peter
2 234.234.234.234 2002/04/23 09:15
3 321.321.321.321 2002/04/24 11:06 John
4 234.234.234.234 2002/04/10 15:15 Mark
5 234.234.234.234 2002/04/09 16:34 Bill
ID is a counter
IP is the address of the user's computer
DATE is the date/time when the user accessed the web server
USER is the name of the user
As you can see from my example, the field USER can be empty. If the user logs in with his name, USER will be updated accordingly, otherwise it will be empty.
THIS IS WHAT I WANT TO DO
On the webpage I want to present information about who has accessed the site. If the USER field in a specific record in the table is empty, the table should be searched for a previous record with the same IP and with the USER field present.
In other words, if the user has not logged in and USER hence is empty, look for a previous login from the *same* IP and use the USER field from that record.
In the example above, record 2, 4 and 5 has the same IP. The information from record 2 should be supplemented with the USER from record 5. The reason to choose 5 and not 4 is to use the *latest* known user of that IP.
THIS IS WHAT I HAVE TRIED
Idea 1: Create a "lookup table" which only contains associated IPs and USERs. Then I join the main table with the lookup table.
SQL:
SELECT t1.id,t1.date,t1.ip,t1.user,t2.user FROM counter t1 LEFT JOIN (SELECT DISTINCT ip,user FROM counter WHERE user<>'') AS t2 ON t1.ip=t2.ip ORDER BY id DESC
The problem with this approach is the the lookup table will contain *several* records with the same IP but different name. When joining with the master table, all combinations between the "master record" and the lookup entries will appear.
In order to get this approach to work, I need to make sure that the lookup table only contains *one* entry per IP. This entry should be the one with the most recent date.
ID IP DATE T1.USER T2.USER
---------------------------------------------------------
5 234.234.234.234 2002/04/09 16:34 Bill Bill
5 234.234.234.234 2002/04/09 16:34 Bill Mark
4 234.234.234.234 2002/04/10 15:15 Mark Mark
4 234.234.234.234 2002/04/10 15:15 Mark Bill
3 321.321.321.321 2002/04/24 11:06 John John
2 234.234.234.234 2002/04/23 09:15 Mark
2 234.234.234.234 2002/04/23 09:15 Bill
1 123.123.123.123 2002/04/24 10:05 Peter
T1.USER is the user from the master table, T2.USER is the lookup name.
The idea was to present the lookup name in those cases the master name was empty.
Ok, this explaination was perhaps a bit long. I hope somwone will be able to help me out!
Cheers,
Rally2002