I trying to write a query to correct an error in a DATETIME field in a table, but my subquery can return NULL as transactiondetails and CreateDate field needs to have a valid value.
My current query is this one:
UPDATE transactionsheaders H SET CreateDate =
CONCAT(
DATE((SELECT CreateDate FROM transactionsdetails WHERE TransactionUID=H.UID GROUP BY TransactionUID)),
' ',
TIME(CreateDate))
WHERE UID>0;
and I want to use the result of
SELECT CreateDate FROM transactionsdetails WHERE TransactionUID=H.UID GROUP BY TransactionUID
if it isn't NULL and DATE(CreateDate) otherwise.
I've been trying to use IF for this purpose, but I am having problems writing something that works...
One more thing, transactionsdetails refers to transactionsheaders through TransactionUID (1 to many). Is GROUP BY guaranteed to return only one result?
Here is the correct thing
UPDATE transactionsheaders H
SET CreateDate = CONCAT(DATE(ifnull((SELECT
CreateDate
FROM transactionsdetails
WHERE TransactionUID = H.UID
GROUP BY TransactionUID)),'2012-02-02'), ' ', TIME(CreateDate))
WHERE UID > 0;
You can set any default value insted of '2012-02-02' or even date(now())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.