I have the following table structure:
table "sample"
objectID | objectValues
1 | 5,6,7
2 | 6,7,5,8
3 | 5
4 | 7,8,9,5,6
5 | 10,11
6 | 5
So, I want to delete all ",5" or "5," values from "objectValues" and delete all columns which theirs "objectValues" value is only "5". How can I do this?
You can try a tricky method using four queries to make this change.
first query for delete all rows when object value is 5
DELETE FROM sample WHERE objectValues = 5;
second for update rows when is a value is in all places except first and last
UPDATE sample
SET objectValues = REPLACE(objectValues , ',5,', ',');
third when the value 5 is on the first place
UPDATE sample
SET objectValues = LEFT(objectValues, LENGTH(objectValues)-2)
WHERE objectValues LIKE '5,%';
and last query when the value 5 is on the last place
UPDATE sample
SET objectValues = RIGHT(objectValues, LENGTH(objectValues)-2)
WHERE objectValues LIKE '%,5';
Try this
delete from table where find_in_set(5,objectValues)>0;
Related
I have a table set up like this (simplified for this example)
ID | int |Primary auto increment
Name | text |
ContainerID | int |
ContainsCount | int |
I am using the container id to refer to the table itself. So if i have a row with
ID:3 Name:Test1 ContainerID:0 ContainsCount:0
ID:4 Name:Test2 ContainerID:3 ContainsCount:0
ID:5 Name:Test3 ContainerID:3 ContainsCount:0
It means that the row 4 and 5 is inside row 3.
I would like to count how many row are inside row 3, and store it in the ContainsCount column.
I know i can get this value manually by doing an additional query for every row that i need to read. But i would like to avoid the strain on the server.
I have tried setting up a trigger that increments the value every time a new row with the corresponding id is added
BEGIN
UPDATE Stock_Items Set Name = "Testing" Where ID = 2;
END
But it turns out i cannot edit the same table using a trigger.
Is there any other way to achive this?
i'm looking to figure out how i can update a row like a LIMIT 2,2 on a select
there is a little example:
col1 | col2
5 10
5 10
5 10
I want to update the 2nd row like this:
col1 | col2
5 10
1 10
5 10
i know the row number so i want something like that:
UPDATE table
SET col1 = 1
WHERE col1 = 5
LIMIT 2, 1
we cant use limit but i know this is achievable, HeidiSQL can do it and i'm trying to figure out how they are doing
thanks
If you need to update particular rows, you need a column or columns that can be used as primary key.
SQL works on sets of rows and you can only update rows that can be identified as belonging to the set.
For example, you can
UPDATE Customers SET Preferred=True WHERE TotalSales > 1000
which will set the "Preferred" flag for any customers that have sales over 1000. This might be one customer or a million or none.
The only way to do the single row update you asked about is to have some way to identify the row. In many database servers you can configure an IDENTITY or SEQUENCE column that will auto-assign each row a unique ID.
You can add an ID column with the IDENTITY property set, which would get you:
ID | col1 | col2
1 5 10
2 5 10
3 5 10
So updating that particular row would be:
UPDATE table SET col1 = 1 WHERE ID = 2
I have a table looks like below:
id name value newColumn
============================
1 Joe 22 null
2 Derk 30 null
newColumn is a newly added column and I would like to update the data for each and every rows with a specific format: concat value and a specific string -CIF
End result should be something like below:
id name value newColumn
============================
1 Joe 22 22-CIF
2 Derk 30 30-CIF
May I know how to construct such update query in mySQL?
This is a fairly basic update:
update table t
set newColumn = concat(value, '-CIF');
I have the following product table
id | companyID | prodID | price | stock | stockAvailable | sumStock
1 A 2 10 5 4
2 B 2 50 10 5
I need to have a trigger when I update a product row that will update the sumStock.
I am new to Triggers, my attempt failed:
CREATE TRIGGER `SalesDB`.`stockSumUpdate` BEFORE UPDATE
ON SalesDB.Product FOR EACH ROW
BEGIN
SET Product.sumStock = Product.stock + Product.stockAvailable
END
My Goal is in this case to calculate the SUM(stock) AS stockSum Where ProductID=2
in this case that would be 15 and then add that to the sumStock. Then add the stockAvailable column to that as well. So in sumStock for both collumns I would have 24.
Result would be:
id | companyID | prodID | price | stock | stockAvailable | sumStock
1 A 2 10 5 4 29
2 B 2 50 10 5 29
Try this syntax:
CREATE TRIGGER `SalesDB`.`stockSumUpdate` BEFORE UPDATE
ON SalesDB.Product FOR EACH ROW
BEGIN
SET NEW.sumStock = NEW.stock + NEW.stockAvailable
END;
That adds within a row of a table.
To get the total, you would use something like:
CREATE TRIGGER `SalesDB`.`stockSumUpdate` BEFORE UPDATE
ON SalesDB.Product FOR EACH ROW
BEGIN
SET NEW.sumStock = (select sum(stock + stockAvailable)
from products where p.prodid = new.prodid and p.id <> id
) + NEW.stock + NEW.stockAvailable;
END;
Except, this still, doesn't do what you want. If you are updating multiple rows at a time, you will get different total stock values.
In other words, you are trying to update a group of rows whenever you update a single row. Gosh, when put like that, it doesn't seem like a good idea. The second set of udpates could update even more rows and so on and so on (it wouldn't happen because of the product).
Instead, create a table with product as a primary key and the stock available in that table (might be an existing table). Then update the summary table every time there is a change in this table.
I have a table where there is a column named "backfill_target" and its value is 1 but I need to change this to 20 for all 300 rows, how would I do this?
EDIT:
Here is a simplified version of my table
ID|name |backfill_target|first_record|second_record
0 | test| 1 |52 |54
try,
UPDATE tableName
SET backfill_target = 20
WHERE backfill_target = 1
Assuming you want to update every row.
UPDATE table SET backfill_target = 20