473,322 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

MySQL Sub query with ORDER BY id DESC LIMIT 1

bilibytes
128 100+
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
Sep 7 '08 #1
3 26682
coolsti
310 100+
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.
Sep 7 '08 #2
bilibytes
128 100+
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.
Sep 7 '08 #3
coolsti
310 100+
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 :)
Sep 8 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Doug | last post by:
I have a pretty long query that ends with ORDER BY R.r_recent_hits DESC LIMIT 0, 1 I also have an index on R.r_recent_hits. I did an explain select and got this: ALL - which means (from...
7
by: Jim | last post by:
I'm using PHP & MySQL to create a simple guestbook. I've created my table and I'm able to load my information in as usual. I would like it to display the latest entry first though. I set an id...
1
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer...
4
by: monomaniac21 | last post by:
hi! is it possible to do the aforementioned query - selecting only distinct in 1 col but retrieving all other cols at the same time. regards marc
1
by: jenson | last post by:
<?php /* * Created on Apr 13, 2007 * * To change the template for this generated file go to * Window - Preferences - PHPeclipse - PHP - Code Templates */ require_once 'init.php'; ...
2
osward
by: osward | last post by:
Hello there, I am using phpnuke 8.0 to build my website, knowing little on php programing. I am assembling a module for my member which is basically cut and paste existing code section of...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
5
by: ScottCase | last post by:
Hello, I have this sql statement and it works fine with mysql v4 $query = "SELECT id, name, jv_signups, mem_signups, ( SELECT SUM( jv_signups ) ) + ( SELECT SUM( mem_signups ) ) AS total...
0
by: Scotsman | last post by:
Hi All I have a sticky problem! Its a LEFT JOIN sql statement which works fine in MySQL but not when called in Perl. Any ideas? I have 2 tables: campaigns & companies. I'm trying to join them...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.