 | 
September 7th, 2008, 02:09 PM
|  | Member | | Join Date: Jun 2008
Posts: 50
| | MySQL Sub query with ORDER BY id DESC LIMIT 1
Hello,
I am trying to make a query with some sub queries, and i am having some syntax errors.
I have a table with the active users:
which contains: id(auto_increment) | user_name | now_active
the now_active field is inserted with value = '1', when the user logs in. And is updated to '0' when the user logs out.
Here is the code: - At login: it inserts
"INSERT INTO users_active (user_name, now_active) VALUES ('$user_name', $user_ip, '1');" - At logout: it updates the last UPDATE users_active SET now_active = '0' WHERE id =(SELECT id IN (SELECT id WHERE user_name = '$user_name') ORDER BY id DESC LIMIT 1);
I have a problem with the second query.
I need mysql to do what is below in one single query:
0. $user_name = John_example
1. $user_ids = "SELECT id FROM users_active WHERE user_name = $username";
2. $user_max_id = "SELECT id WHERE id = $user_ids ORDER BY id DESC LIMIT 1"
3. $update = "UPDATE users_active SET now_active = '0' WHERE id = $user_max_id;
the point is that I want to do this in one single query...
if its possible.
if you have any suggestions they are wellcome
thankyou
bilijames
| 
September 7th, 2008, 03:49 PM
| | Needs Regular Fix | | Join Date: Mar 2008
Posts: 305
| |
Have you tried this?
UPDATE users_active T1 SET T1.now_active = '0' WHERE T1.id =(SELECT T2.id from users_active T2 where T2.user_name='$user_name' order by T2.id desc limit 1);
I do not know if this will work because MySQL does not like to have the same table in a subquery that is being updated in the main query, but you can try it and see what happens. Otherwise you need another work around.
If this works, this will only update the row for the user $user_name which corresponds to the highest ID value for that user. Is this what you are trying to do?
You might want to rethink your problem, though, before you proceed with the MySQL part. What is it you really want to do? The problem with anything like this is that a user does not necessarilly need to log out. He/she can just walk away from the application. Maybe this is what you are trying to account for, where all the times that the user did not log out you have the value of users_active still equal to 1.
If you are trying to make a database table as a log of users that log in and then log out (or not) then I would do it another way. On logging in to the application, I would find out the ID that was created in this table (by the auto_increment column) when the user logs in, and store that ID in the application. If for example PHP, then store it as a session variable. Then if the user logs out, you can - more easilly - update directly the correct row in the database because you know the ID.
| 
September 7th, 2008, 05:42 PM
|  | Member | | Join Date: Jun 2008
Posts: 50
| | Quote: |
Originally Posted by coolsti Have you tried this?
UPDATE users_active T1 SET T1.now_active = '0' WHERE T1.id =(SELECT T2.id from users_active T2 where T2.user_name='$user_name' order by T2.id desc limit 1);
I do not know if this will work because MySQL does not like to have the same table in a subquery that is being updated in the main query, but you can try it and see what happens. Otherwise you need another work around.
If this works, this will only update the row for the user $user_name which corresponds to the highest ID value for that user. Is this what you are trying to do?
You might want to rethink your problem, though, before you proceed with the MySQL part. What is it you really want to do? The problem with anything like this is that a user does not necessarilly need to log out. He/she can just walk away from the application. Maybe this is what you are trying to account for, where all the times that the user did not log out you have the value of users_active still equal to 1.
If you are trying to make a database table as a log of users that log in and then log out (or not) then I would do it another way. On logging in to the application, I would find out the ID that was created in this table (by the auto_increment column) when the user logs in, and store that ID in the application. If for example PHP, then store it as a session variable. Then if the user logs out, you can - more easilly - update directly the correct row in the database because you know the ID. | Thank you very much!
You really focused on what I wanted to do.
I have tried your query and it doesn't work the error is this: "You can't specify target table 'users_active' for update in FROM clause".
But reading the last part of your comment, i realized that your approach was much better, so i have decided to use it.
As I generally can't know which approach is the less resource consuming, I often get into these type of troubles trying to make wrong ones.
Thank you again.
| 
September 8th, 2008, 12:27 PM
| | Needs Regular Fix | | Join Date: Mar 2008
Posts: 305
| |
Glad I could help!
The error you got when trying my query is what I expected. You cannot update a table that is also being selected from in a subquery. But I think there is another syntax for doing this, without needing to use the subquery.
But my suggested approach afterwards seems to do the trick for you.
It always pays, when posting a question here on these forums, to explain your entire problem (briefly) rather than just asking for a solution to, for example, a syntax error. Because one of us can maybe then understand what you are trying to do and suggest a solution that avoids your original problem :)
|  |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|