I have a table called "allarmi_ingressi" in SQLite, with a lot of rows in it.
I want to create a query that changes the variable on my column "visto" to 1, if "visto=0", and to 0, if "visto=1".
This is what i made:
UPDATE allarmi_ingressi SET visto = '1' WHERE visto = '0'
Of course this modify every row in the column "visto";
I want to know if it's possible to modify it "selecting" it by the primary key, in my case "id_allarme".
In a SELECT query, you would use the WHERE clause to find rows with a specific id_allarme value.
The same WHERE clause can be used with UPDATE:
UPDATE allarmi_ingressi
SET visto = 1 - visto
WHERE id_allarme = ?;
Use CASE Expression
Query
update allarmi_ingressi
set visto = (
case visto when '1' then '0'
when '0' then '1'
else visto end
)
where id_allarme = __; -- id here
Related
How do I append a character to all items `purchase_id' here is a manual example of what I want...
SELECT *
FROM `loadable_link`
WHERE `product_sku` = '2101-R'
ORDER BY `customer_id` DESC
Then select from purchased_id and append a '0' to all purchased ID's
UPDATE `loadable_link` SET `purchased_id` = '11165690'
WHERE `loadable_link`.`purchased_id` = 1116569;
You can update the table according to the condition in the original select statement.
If purchase_id is a number, you can multiply it by 10:
UPDATE `loadable_link`
SET `purchase_id` = `purchase_id` * 10
WHERE `product_sku` = '2101-R'
If purchase_id is a string, you can concatenate a 0 to it:
UPDATE `loadable_link`
SET `purchase_id` = CONCAT(`purchase_id`, '0')
WHERE `product_sku` = '2101-R'
UPDATE `loadable_link`
SET `purchased_id` = CONCAT(`purchased_id`, "0")
WHERE `product_sku` = "2101-R";
This can be achieved in one UPDATE query, take the original value of each row and CONCAT() to append a 0 to the end of the existing purchase_id.
Iam struck up with a query. When i edit a data in Table A, it needs to check if the same data is present in Table B, If data is present in Table B, just ignore updation, if data is empty or Null, then the data from Table A needs to Update in Table B. With the following query iam almost achieving it but the issue is When the Data is present in Table B, its just deleting the data. It should actually ignore it when data is present. A small issue with the case statement i guess. Pls help me on this.
$strSQLInsert2 = "UPDATE Table B
SET
`tender_intendername` = CASE WHEN `tender_intendername`='' or `tender_intendername` IS NULL
THEN '".$values["intendername1"]."' END,
`no_of_participants` = CASE WHEN `no_of_participants`='' Or `no_of_participants` IS NULL
THEN '".$values["no_of_participants"]."' END
WHERE tender_id=" . $values["tender_id"];
You have to add an ELSE to the CASE expression:
UPDATE Table B
SET `tender_intendername` = CASE
WHEN `tender_intendername`='' or
`tender_intendername` IS NULL
THEN '".$values["intendername1"]."'
ELSE `tender_intendername` END,
`no_of_participants` = CASE
WHEN `no_of_participants`='' Or
`no_of_participants` IS NULL
THEN '".$values["no_of_participants"]."'
ELSE `no_of_participants` END
WHERE tender_id=" . $values["tender_id"];
The ELSE statement sets each field equal to itself, hence it guarantees that the field will not change when the field is not empty and not NULL.
I solved it. But need to write two queries to achieve the result.
$strSQLInsert2 = "UPDATE `TableB`
SET
`tender_intendername` = '".$values["intendername1"]."'
WHERE
`tender_intendername` = ''
OR `tender_intendername` IS NULL AND tender_id=" . $values["tender_id"];
I have this MERGE statement working fine:
MERGE INTO product_replenishment PR
USING ( SELECT * FROM traxs_temp..__MinMaxImport WHERE session_id = #session_id ) T
ON T._product = PR.product AND T._branch_no = PR.branch_no
WHEN MATCHED AND T._stock_warehouse IS NOT NULL THEN
UPDATE SET
date_time_updated = GETDATE(),
user_no_updated = #user_no,
stock_warehouse = T._stock_warehouse
WHEN NOT MATCHED BY TARGET AND T._stock_warehouse IS NOT NULL THEN
INSERT
(date_time_created,
date_time_updated,
user_no_created,
user_no_updated,
branch_no,
product,
stock_warehouse,
archive)
VALUES
(GETDATE(),
GETDATE(),
#user_no,
#user_no,
T._branch_no,
T._product,
T._stock_warehouse,
0);
I want to add another WHEN MATCHED statement like this:
WHEN MATCHED AND T._stock_warehouse IS NOT NULL THEN
UPDATE SET
date_time_updated = GETDATE(),
user_no_updated = #user_no,
stock_warehouse = T._stock_warehouse
WHEN MATCHED AND T._stock_warehouse IS NULL THEN
UPDATE SET
date_time_updated = GETDATE(),
user_no_updated = #user_no,
archive = 1
But I get an error: An action of type 'WHEN MATCHED' cannot appear more than once in a 'UPDATE' clause of a MERGE statement.
Is it impossible to achieve what I'm trying to do?
The general case can be emulated using CASE expressions as I've shown in this answer here, or in this blog post here. But your case is more specific, because the difference between the two clauses is simpler than the general case. You can combine them into one clause:
WHEN MATCHED THEN UPDATE SET
-- These are always updated the same way, regardless of the WHEN MATCHED AND predicate
date_time_updated = GETDATE(),
user_no_updated = #user_no,
-- These depend on the relevant WHEN MATCHED AND predicate, so use CASE
stock_warehouse = CASE
WHEN T._stock_warehouse IS NOT NULL THEN T._stock_warehouse
ELSE stock_warehouse
END,
archive = CASE WHEN T._stock_warehouse IS NULL THEN 1 ELSE archive END
I am trying to LEFT JOIN 2 tables. which is working out fine. But i am getting back two sets of fields named setting_value. iam trying to get tblSettings.setting_value only if tblAgencySettings.setting_value is NULL. How would i go about doing this? I know i can rename the fields, then in PHP i can check the tblAgencySettings.setting_value and if NULL then grab the tblSettings.setting_value but i prefer to keep this at MySQL.
SELECT `tblSettings`.`id`, `tblSettings`.`setting_name`,
`tblSettings`.`setting_value`, `tblAgencySettings`.`setting_value`
FROM `tblSettings` LEFT JOIN `tblAgencySettings`
ON `tblSettings`.`id` = `tblAgencySettings`.`setting_id`
AND `tblAgencySettings`.`agency_id` = '1'
WHERE `tblSettings`.`changeable` = '1'
slight issue i just noticed. i failed to mention this. if tblAgencySettings.setting_value does have a value. but changeable is not 1 then just select tblSettings.setting_value
Just add a COALESCE:
SELECT `tblSettings`.`id`, `tblSettings`.`setting_name`,
COALESCE(`tblAgencySettings`.`setting_value`, `tblSettings`.`setting_value`)
FROM `tblSettings` LEFT JOIN `tblAgencySettings`
ON `tblSettings`.`id` = `tblAgencySettings`.`setting_id`
AND `tblAgencySettings`.`agency_id` = '1'
WHERE `tblSettings`.`changeable` = '1'
The COALESCE function returns the first non-NULL value you give it so this:
COALESCE(`tblAgencySettings`.`setting_value`, `tblSettings`.`setting_value`)
Will be tblAgencySettings.setting_value if that's not NULL and tblSettings.setting_value if tblAgencySettings.setting_value is NULL.
If tblAgencySettings.setting_value can also be zero and you want to ignore that as well as NULL, then you could use this instead of the COALESCE above:
COALESCE(
IF(`tblAgencySettings`.`setting_value` = 0, NULL, `tblAgencySettings`.`setting_value`),
`tblSettings`.`setting_value`
)
The IF returns the second argument if the first is true and the third if the first argument is false so the above use converts zero to NULL. Or, you could go all the way to a CASE statement:
case
when `tblAgencySettings`.`setting_value` = 0 then `tblSettings`.`setting_value`
when `tblAgencySettings`.`setting_value` IS NULL then `tblSettings`.`setting_value`
else `tblSettings`.`setting_value`
end
Change your SQL Statement to this:
SELECT `tblSettings`.`id`, `tblSettings`.`setting_name`,
CASE WHEN `tblSettings`.`setting_value` IS NULL THEN `tblAgencySettings`.`setting_value`
ELSE `tblSettings`.`setting_value` END AS `setting_value`
FROM `tblSettings` LEFT JOIN `tblAgencySettings`
ON `tblSettings`.`id` = `tblAgencySettings`.`setting_id`
AND `tblAgencySettings`.`agency_id` = '1'
WHERE `tblSettings`.`changeable` = '1'
Here's a link to MYSQL CASE Statement for your reference.
I am trying to use a mysql case query to update multiple rows in a table. I have some cases which need to update the row with the same value. I was wondering whether it is possible to put all of these into one case or whether I have to create a new 'WHEN' for each?
Below is an example of what I am trying to accomplish but it obviously isn't the correct way to do this because I get an error.
UPDATE `groups` SET `status` = CASE `group_id`
WHEN 32 OR WHEN 33 THEN '1'
WHEN 31 THEN '2'
END
Is it possible to do something like that?
Thanks
I think you want
UPDATE groups SET status = CASE
WHEN group_id = 32 OR group_id = 33 THEN '1'
WHEN group_id = 31 THEN '2'
END
Edit You can use operators like BETWEEN. For example
UPDATE groups SET status = CASE
WHEN group_id BETWEEN 32 AND 33 THEN '1'
WHEN group_id BETWEEN 30 AND 31 THEN '2'
END
TRY
UPDATE `tablename` SET `status`= IF('group_id=31',2,1)
EDIT
UPDATE tableName SET `status` = IF( group_id IN (31, 32), 2, 1 ) WHERE section_id=1
OR
UPDATE tableName SET `status` = IF( group_id ANY (31, 32), 2, 1 ) WHERE section_id=1
running successfully on my table..what error u facing??
other syntax
UPDATE `tableName` SET `group_id` = CASE
WHEN group_id IN (31,32) THEN 1
WHEN group_id IN (33,34) THEN 2
END