I have two tables: chapter and updates
I have an UPDATE query to set member ids from the updates table into the chapter table. The problem I am trying to find a syntax solution to is that I need to use one query, but have it NOT update (skip) the value if the update value is '0'. When an update is filed (to await UPDATE processing), not all ids are changed, and those that are not are saved into my updates table as '0' while valid changes are a seven digit integer. The problem arises when an UPDATE is applied, any existing ids are overwritten with the '0' when that field should actually have its existing value retained. A sample of my current query is:
UPDATE chapter
SET chapter.election_date = updates.election_date,
chapter.president = updates.president_id,
chapter.vice_president = updates.vice_president_id,
chapter.secretary = updates.secretary_id,
WHERE
updates.chapter_id = chapter.id
AND updates.installation_date < CURRENT_DATE ()
Based off of this example, I am trying to find a way to have chapter.president NOT be updated if updates.president_id = '0'
If this is doable, any help or guidance would be appreciated.
Just add this condition to the join clause:
UPDATE chapter
JOIN updates ON updates.chapter_id = chapter.id AND
updates.president_id != '0'
SET chapter.election_date = updates.election_date,
chapter.president = updates.president_id,
chapter.vice_president = updates.vice_president_id,
chapter.secretary = updates.secretary_id,
WHERE updates.installation_date < CURRENT_DATE ()
The below query will not update the chapter.president, if updates.president_id=0, but will update all other fields.
UPDATE chapter, updates
SET chapter.election_date = updates.election_date,
chapter.president = if(updates.president_id<>0,updates.president_id,chapter.president)
chapter.vice_president = updates.vice_president_id,
chapter.secretary = updates.secretary_id,
WHERE
updates.chapter_id = chapter.id
AND updates.installation_date < CURRENT_DATE()
Related
si have a db field that i want to use to track intervals. i want to push completed intervals onto the db field when they are completed. to wit:
intervals = '10'
intervals = '1020' <-- pushing 20 onto the field
intervals = '102040' <-- pushing 40 onto the field
intervals = '102040SP' <-- pushing SP onto the field
the values will never decrement (and order doesn't really matter, if that's a factor), so i'm only looking for a way to UPDATE the field, but i have no idea how to do that because UPDATE tbl SET ... just overwrites the existing contents. i looked into CONCAT, but that works with variables the user provides, not with existing data AND additional user data. if i were to write some PSEUDO code, it might look like this:
UPDATE tbl PUSHTO intervals VALUE newInterval WHERE id='id' AND date='date'
so. can anybody help me out here? there has to be a way to do this. :)
An update with concatenation is what you want here:
UPDATE tbl
SET interval = CONCAT(interval, newInterval)
WHERE id = 'id' AND date = 'date';
If you wanted to make the update even in the event that newInterval might be null, you could try:
UPDATE tbl
SET interval = CONCAT(interval, COALESCE(newInterval, ''))
WHERE id = 'id' AND date = 'date';
What I currently have is:
UPDATE card, records
IF(records.date_returned == null) THEN SET
card.last_seen = records.date_loaned
ELSE SET card.last_seen = records.date_returned
WHERE card.card_no = records.card_no
A little background-- the table records has two columns-- date_loaned and date_returned, with date_returned set as null by default. I was wondering whether its possible to change the last_seen column in temp_card to date_returned when it gets updated
Pretty sure its impossible, but I guess I'm trying my luck!
I was kinda hoping it to be automatic (e.g. when records get updated, this triggers the last_seen to change).
You can try like this..
Update Card A INNER JOIN Record B ON (A.card_no =B.card_no) SET A.last_seen =(
Case WHEN B.date_returned==null
then B.date_loaned
Else B.date_returned
End
)
How about using CASE like
UPDATE card, records
SET
card= CASE records.date_returned == null
THEN card.last_seen = records.date_loaned
ELSE SET card.last_seen = records.date_returned
WHERE card.card_no = records.card_no
I have a permission system between two objects (users => firms) with table permissions for linking. Now i need to update firms table with first permission user id. I made this query:
UPDATE parim_firms, parim_permissions
SET parim_firms.firm_user_id = parim_permissions.permission_a_id
WHERE parim_firms.firm_user_id = 0
AND parim_firms.firm_id = parim_permissions.permission_b_id
Now if one firm hash multiple linked users, then will it be updated with the first or last matched user?
My logic says after first update firm_user_id != 0 and that row doesn't get updated anymore.
But im not sure, maybe does it run the query for all joined rows and the last row will stay.
And if it doesn't then how can i modify the query to update with only first matched result?
UPDATE parim_firms
SET parim_firms.firm_user_id =
(
select parim_permissions.permission_a_id from parim_permissions
WHERE parim_firms.firm_id = 0
AND parim_firms.firm_id = parim_permissions.permission_b_id
)
or
update parim_firms a
set a.firm_user_id = b.permission_a_id
from parim_permissions b
WHERE parim_firms.firm_id = 0
AND parim_firms.firm_id = parim_permissions.permission_b_id
I'd like to update the value of a field only if the new one is lower. I tried min like this, without success
UPDATE `editions` SET `editions`.`year` = MIN(`editions`.`year` , '2005') WHERE `editions`.`id` = 1;
I get a 1064 error. How can I update the value without retrieving the value first?
UPDATE `editions`
SET `editions`.`year` =
LEAST(`editions`.`year` , '2005') WHERE `editions`.`id` = 1;
Have you tried doing it like this:
update editions set year = 2005 where id = 1 and year > 2005;
(i.e. restrict the selection to row(s) that you would want updated)
I have a question regarding updating a MySQL database.
I have three tables: Match, Submission and SubmissionVersion. A SubmissionVersion can be set as 'Favorite'. But I can't just query UPDATE SubmissionVersion SET IsFavorite = 1 WHERE ID = $ID because of the relation to a Submission and than the Match. My question is how can I update a SubmissionVersion column with a MySQL Query with two joins? I've tried this query but I can't get it to work.
UPDATE
SubmissionVersion
JOIN
Submission
ON
Submission.ID, SubmissionVersion.SubmissionID
JOIN
Match
ON
Match.ID ON Submission.MatchID
SET
SubmissionVersion.IsFavorite = ".$Index."
WHERE
SubmissionVersion.ID = ".$ID."
AND
Match.ID = ".$MatchID
UPDATE SubmissionVersion sv
SET sv.IsFavorite = ".$Index."
WHERE sv.ID = ".$ID."
AND sv.ID IN (
SELECT s.ID
FROM Submission s
WHERE s.MatchID = ".$MatchID'")
If I understand your statement correctly, that should work.
I eliminated the Match table completely since you're just checking the value against a column in Submission.
Let's start with saying that MATCH is MySQL's reserved word, so needs to be put into backticks.
http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html