我有一个对象表:
object_id -- active -- tarif_id
1 1 5
2 0 6
3 1 19
我有一张tarifs表:
tarif_id -- expire_date
5 2014-01-01
6 2014-05-01
19 2015-12-11
是否可以编写一个SQL,用object's tarif_id's
检查所有Date.NOW
过期日期,如果Date.Now
更多,则将object's active
状态设置为{{1 }}?
答案 0 :(得分:1)
当然可以。您正在寻找UPDATE
功能。
UPDATE ObjectsTable
SET active = 0
WHERE [tarif_id] IN (
SELECT [tarif_id]
FROM TarifTable
WHERE [expiredate] < DATE.Now --or whatever you are using to get the current date
)
答案 1 :(得分:1)
试试这个
UPDATE tblObject
INNER JOIN tbltarifs ON tblObject.tarif_id = tbltarifs.tarif_id
SET tblObject.active = 0
WHERE DATE_FORMAT(NOW(),'%m-%d-%Y') > DATE_FORMAT(expire_date,'%m-%d-%Y')