简体   繁体   中英

mysql update query syntax

I'm trying to use the following query syntax from a php file:

$sql = "UPDATE properties SET properties.ht_hs = 3.5 WHERE properties.oil_data_id = acea.oil_data_id AND (acea.ACEA_A3 = 1 OR acea.ACEA_B3 = 1 OR acea.ACEA_B4 = 1) AND properties.ht_hs < 3.5";

$result = mysql_query($sql) or die(mysql_error());

However, It's not doing what I want. I have two tables in my database, and I need to change the value of some of the records for one column within one of the tables (the ht_hs column/field within the properties table). However,the criteria for when to change that field is dependent upon both tables.

Each motor oil in the database has an id, which is listed in the "oil_data_id" column of each table.

I'm trying to find oils that meet the ACEA A3 or B3 or B4 spec (ie, they have a "1" in that column of the acea table) which also have a value of less than 3.5 in the ht_hs column of the properties table.

If they do, I want to update the value to 3.5.

How can I restructure my query so that it works?

I think you're looking for something like this:

UPDATE properties
SET properties.ht_hs = 3.5
WHERE properties.oil_data_id in
    (select acea.oil_data_id
     from acea
     where (acea.ACEA_A3 = 1 OR acea.ACEA_B3 = 1 OR acea.ACEA_B4 = 1))
AND properties.ht_hs < 3.5;

You would need to include table acea in the JOIN like :-

UPDATE properties, acea
SET ...;

See the documentation

UPDATE items,month SET items.price=month.price
WHERE items.id=month.id;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM