I want to update "status" column. I wanted to use UPDATE with IF condition something like this:
UPDATE rent_record
SET status = IF(status='pending', borrowed, IF(status='returnP', returned) )
WHERE WHERE ID='$name'
I am not sure if it'll be possible. Please suggest the best possible ways to do this.
Thank you
Your query is almost right, you need to add an else value for the second IF, which should probably be status i.e.:
UPDATE rent_record
SET status = IF(status='pending', 'borrowed', IF(status='returnP', 'returned', status))
WHERE ID='$name'
You should use CASE:
UPDATE rent_record
SET status = CASE WHEN status = 'pending' THEN 'borrowed'
WHEN status = 'returnP' THEN 'returned'
ELSE ...
END
WHERE ID=?
Related
I have a table named TableX in MySQL. There are 4 columns in TableX. The columns are ColumnCompare_Now, ColumnCompare_Past, ColumnNumber_Now, ColumnNumber_Past.
I want to write a MySQL statement that has the following logic;
If ColumnCompare_Now == 'ActionNeeded' and ColumnCompare_Past == 'ActionNeeded',
then ColumnNumber_Now = `ColumnNumber_Now` + `ColumnNumber_Past`
MySQL knowledge is limited to simple Select Where statements. How can the above operation be implemented in MySQL? Meanwhile, I will try to figure this out myself while waiting for help from fellow members of StackOverflow.
I'm not completely sure I know what you're trying to accomplish. If you want to select a new column with this information, you can use a case statement:
select
case when ColumnCompare_Now = 'ActionNeeded'
and ColumnCompare_Past = 'ActionNeeded'
then ColumnNumber_Now + ColumnNumber_Past
else ColumnNumber_Now
end as ColumnNumber_Now
from tablex
And if you need this in an update query:
update tablex
set ColumnNumber_Now = ColumnNumber_Now + ColumnNumber_Past
where ColumnCompare_Now = 'ActionNeeded'
and ColumnCompare_Past = 'ActionNeeded'
update TableX set ColumnCompare_Now=(select (ColumnNumber_NOw+ColumnNumber_Past) where ColumnCompare_Now=='ActionNeeded and ColumnCompare_Past=='ActionNeeded');
kindly try out this.
I am using an UPDATE query to update multiple rows which is something like the following
UPDATE TBLNAME SET NEWCOLUMN =
CASE
WHEN TYPE='ACCOUNT' THEN 'FINANCE'
WHEN CODE='DATA' AND CLASS='FIRST' THEN 'LEGAL'
END
The above query works fine for single conditions and for certain conditions something like
NEWCOLUMN = Audit when Type = file, fax, documents, I was using something like
UPDATE TBLNAME SET NEWCOLUMN =
CASE
WHEN TYPE='ACCOUNT' THEN 'FINANCE'
WHEN CODE='DATA' AND CLASS='FIRST' THEN 'LEGAL'
WHEN TYPE='FILE' AND 'FAX' AND 'DOCUMENTS' THEN 'AUDIT'
END
The above query works fine for the first two conditions but the third condition AUDIT is not updated.
Could somebody help?
Thanks
Use
WHEN TYPE IN ('FILE','FAX','DOCUMENTS') THEN 'AUDIT' END
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 MySQL UPDATE statement that uses a CASE clause
UPDATE partsList SET quantity =
CASE
WHEN partFK = 1 THEN 4
WHEN partFK = 2 THEN 8
END
WHERE buildFK = 1;
The above statement works. Yet when I remove one of the WHEN statements, it breaks and the error indicates the CASE clause isn't returning anything. Is it that the CASE clause must have more than one WHEN to function.
I don't know beforehand how many updates I'll need, so I'm trying to build a single update statement that can handle one or many updates.
Thanks for any insights you can provide.
It isn't that the CASE must have more than one, WHEN...THEN, it's that it must handle all the data you give it.
If you removed one of the clauses, you leave a hole. e.g.
UPDATE partsList SET quantity =
CASE
WHEN partFK = 1 THEN 4
END
WHERE buildFK = 1;
With this update statement, if parkFK is 2, then the update fails because the CASE can't handle the input.
You can either limit your source data by adding another line to your where-clause (e.g. AND partFK in (1,2)), or you could add an ELSE to the case expression.
UPDATE partsList SET quantity =
CASE
WHEN partFK = 1 THEN 4
WHEN partFK = 2 THEN 8
ELSE 12
END
WHERE buildFK = 1;
However, based on the SQL statement you've shown, there is probably a better way. Presumably, partFK is a foreign-key to some other table. Can you pull the value for quantity from there?
Add ELSE NULL or something before the END of the case. See http://dev.mysql.com/doc/refman/5.0/en/case-statement.html for more info.
MS SQL Server version of sql statement would be as following:
Update partsList SET quantity =
CASE partFK
WHEN 1 THEN 4
WHEN 2 THEN 8
END
I have an UPDATE Query :
UPDATE FKMS_GNST_Transaction_Details
SET Received_Quantity=Received_Quantity+(
CASE
WHEN (#int_Updated_Qty)>=(GTD.Quantity-GTD.Received_Quantity)
THEN GTD.Quantity-GTD.Received_Quantity
ELSE (#int_Updated_Qty)
END)
,#int_GNST_Reference_Id=GTD.Transaction_Detail_Id
FROM FKMS_GNST_Transaction_Details GTD
INNER JOIN #tbl_transactions tmp
ON tmp.Transaction_id=GTD.Transaction_id
AND GTD.Item_id=tmp.Item_id
I want to get the quantity which is added to the Received_Quantity field. That is, if (#int_Updated_Qty)>=(GTD.Quantity-GTD.Received_Quantity) then GTD.Quantity-GTD.Received_Quantity other wise #int_Updated_Qty.
How can we take this value (into a variable or any other way)? Please help.
Use the OUTPUT clause
UPDATE FKMS_GNST_Transaction_Details
SET Received_Quantity=Received_Quantity+(
CASE
WHEN (#int_Updated_Qty)>=(GTD.Quantity-GTD.Received_Quantity)
THEN GTD.Quantity-GTD.Received_Quantity
ELSE (#int_Updated_Qty)
END)
,#int_GNST_Reference_Id=GTD.Transaction_Detail_Id
--start gbn code
OUTPUT INSERTED.Received_Quantity
--end gbn code
FROM FKMS_GNST_Transaction_Details GTD
INNER JOIN #tbl_transactions tmp
ON tmp.Transaction_id=GTD.Transaction_id
AND GTD.Item_id=tmp.Item_id
The OUTPUT results can go
into a table (real, temp or variable)
to the client as a recordset
You can't assigned directly to a local variable