Jump to content

Long old UPDATE statement


biffta

Recommended Posts

I have the following SQL which gives me the row Im after

 

SELECT *

FROM staff, staffemails

WHERE staff.users_username=\'$user\'

and staff.staff_id = staffemails.staff_id

 

thing is I now want to perform an update on the result of this query. Ive tried

 

UPDATE staffemails

SET is_registered =\'0\'

WHERE staff.users_username = \'$user\'

and staffemails.staff_id = staff.staff_id

 

But that doesnt work! :(

 

Any ideas????

Link to comment
Share on other sites

hmmm.. only thing i can think of is that the \"is_registered\" column hold numbers only.. so try

 


UPDATE staffemails 

SET is_registered =0

WHERE staff.users_username = \'$user\' 

and staffemails.staff_id = staff.staff_id 

 

Lets see if it works this way....

 

--goodluck

Link to comment
Share on other sites

No good sir. is-registered is a set so the quotes are needed!

 

The actual error MySql gives me is

Unknown table \'staff\' in where clause
which is definately not the case!

I believe the problem lies with the fact that I am using two tables and and update statement usually uses only one???

Link to comment
Share on other sites

With mysql updates are limited to a single row, or multiple columns if you specify a WHERE clause that includes a constant. Typically the way to do what you\'re trying to do would be something involving a subquery. I know that mysql is somewhat in the process of adding subqueries but I\'m not 100% current with version 4.x stuff now.

 

The query would be structured like this (if subqueries were working):

 


UPDATE staffemails

SET is_registered = \'0\'  (quotes not needed if is_registered is a numeric column type)

WHERE staff_id = (select staff_id from staff where username=\'$user\')

 

Alas they are not available to the best of my knowledge.

 

I do question your premise though. You state that this query gives you your \"row\".

 

SELECT *

FROM staff, staffemails

WHERE staff.users_username=\'$user\'

and staff.staff_id = staffemails.staff_id

 

 

It seems that all you are trying to do is shortcut your need to determine the user\'s staff_id. Simply select that first as I did in my subquery and do your simple update with the right staff_id in hand. It does take 2 queries, but they are short and should operate very quickly, especially if you are using the mysql_pconnect.

 

Even if you aren\'t mysql\'s connections are lightweight and pretty efficient.

Link to comment
Share on other sites

The MySQL syntax to update one table while joining to another is a bit different. It goes something like this:

 

UPDATE staffemails e, staff s

SET e.is_registered =\'0\'

WHERE s.users_username = \'$user\'

and e.staff_id = s.staff_id;

 

BTW, this works in MySQL 4.0.4 or later.

 

Joe

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.