UPDATE `users` SET `D1`='hello ['firstname']' WHERE id_user >= 3
I want to update the D1 column with "hello ['firstname']"
I want to fetch firstname from another column with same row number. and the above syntax is not working out. I'm new to ášhis. Is this possible? or not?
You can use CONCAT for that purpose, and you can so combine strings together
UPDATE `users` SET `D1`= CONCAT('hello [',`firstname`,']') WHERE id_user >= 3
Do you want to construct a string with firstname -- the column -- in it?
If so:
UPDATE users
SET D1 = CONCAT('hello', firstname')
WHERE id_user >= 3;
Related
I'm trying to fill a certain column of a SQL table with data from another table. I have a column named "size" in my table which should return the number of rows in the 2nd table where the id of both rows is the same. Is there a way to populate a SQL column based on a certain command? I would love to be able to fill the column based on this command:
SELECT count(*)
FROM second_table
WHERE id = "row_id";
Here is a sample database with the two tables:
Table 1
Name
id
tiger
1
lion
2
gazelle
1
Here is the desired output for Table 2:
id
Number of Animals
1
2
2
1
I am trying to fill the Number of Animals column but do it automatically and dynamically when another row is added or deleted to Table 1, which is why I want the Select count(*) SQL statement as the code for the column.
One method is a correlated subquery:
update table1 t1
set size = (select count(*)
from table2 t2
where t2.id = t1.id
);
If you need to do this dynamically (as data is inserted), then you would need to use a trigger. However, I would suggest that you calculate the value as needed, unless there is a specific reason why you need to store it.
I guess you need something like this:
CREATE TRIGGER UpdateAnimalCountTable2
AFTER INSERT ON `Table1` FOR EACH ROW
begin
DECLARE NewCount int;
SELECT count(1)
INTO #NewCount
FROM Table1
WHERE Table1.id= NEW.id;
UPDATE Table2
SET NoOfAnimals = #NewCount
WHERE id = NEW.id;
END;
Above is the trigger which will be executed after every insert in Table1 and will update the count in Table 2 for ID which just got inserted in Table1.
I have a table with 10 rows which contains a column named "age" and holds int(3). Is there a way to update all the rows with everyone's respective ages with one SQL statement?
I've been doing
update table_name set age = age_int where first_name = "John";
over and over again. But I'm wondering if I can condense all 10 statements into one.
You could use a case expression:
UPDATE table_name
SET age = CASE first_name
WHEN 'abc' THEN 10
WHEN 'xyz' THEN 11
END
I've being trying to amend the solution found in this tutorial to write an SQL query that both SELECTs and UPDATEs my table:
enter link description here
DECLARE #column1 varchar(2);
SET #column1 = (SELECT `Id`, `Url` FROM `MyTable` WHERE `Retrieved` = 0);
SELECT * FROM `MyTable` WHERE `AdId`, `Url` = #column1;
UPDATE `MyTable` SET `Retrieved` = 1 where `Id`, `Url` = #column1;
What i'm trying to achieve the following simultaneously:
SELECT Id, Url FROM MyTable WHERE Retrieved = 0
UPDATE MyTable SET Retrieved = 1
for the rows where i have SELECTed the results from
Basically, i want to select all data from ID and Url columns where the Retrieved column equals 0. I then want to set the Retrieved column to 1 for the rows i have selected.
The "normal" SQL method would be:
UPDATE MyTable
SET Retrieved = 1
WHERE id IN (SELECT Id FROM MyTable WHERE Retrieved = 0);
That does not work in MySQL. Assuming that id is unique in MyTable (a reasonable assumption in my opinion), then this does what you want:
UPDATE MyTable
SET Retrieved = 1
WHERE Retrieved = 0;
UPDATE t SET t.Retrieved=1 FROM MyTable t WHERE t.Retrieved=0
This is only updating rows, that essentially you've selected. In your case you want to update rows where the selected rows Retrieved column is equal to 0.
The other thing and maybe for readibility or you need the rows returned you can use a cte.
--first get only the records you need
WITH MyRecords_cte
AS
(
SELECT Id, URL, Retreived FROM MyTable WHERE Retreived=0
)
UPDATE MyRecords_cte SET MyRecords_cte.Retreived=1
Once you're done with the update you can return the data.
I'm trying to update a certain column of certain row WHERE id is certain value. The thing is, the number/names of columns are variable, and so are their respective ids.
For example:
UPDATE table SET column1="hello" WHERE id = 5
UPDATE table SET column2="cucumber" WHERE id = 6
How can I do a single mysql query in PDO to do this?
First thing I tried is...
UPDATE table SET column1="hello", column4="bye" WHERE id IN(5, 6)
But that query will update BOTH of those columns in rows where it finds BOTH of those ids, and that's not what I'm looking for. Is it only possible to do this query by query?
Keep in mind that the argument after SET is variable, so the columns to be updated, their values and their respective ids are also variable.
A solution where you can just purely bind values would be great, but if I have to build the query string with escaped variables, then that's OK too.
Thank you.
You can do this
UPDATE table t1 JOIN table t2
ON t1.id= 5 AND t2.id= 6
SET t1.column1= 'hello',
t2.column2 = 'cucumber';
Or if you want to do this on a single column
UPDATE table
SET column2 = CASE id
WHEN 5 THEN 'hello'
WHEN 6 THEN ''
END
WHERE id IN(5, 6);
How can I store only 10 rows in a MySQL table? The older rows should be deleted when a new row is added but only once the table has 10 rows.
Please help me
You could achieve this with an after insert trigger, delete the row where it is min date. e.g. DELETE FROM myTable WHERE myTimestamp = (SELECT MIN(myTimestamp) FROM myTable) but that could in theory delete multiple rows, depending on the granularity of your updates.
You could have an incrementing sequence, and always just delete the min of that sequence.
The question is why you'd want to do this though? It's a slightly unusual requirement.
A basic example (not validated/executed, I don't have mySQL on this particular machine) would look something like.
CREATE TRIGGER CycleOldPasswords AFTER INSERT ON UserPasswords FOR EACH ROW
set #mycount = SELECT COUNT(*) FROM UserPasswords up where up.UserId = NEW.UserId;
if myCount >= 10 THEN
DELETE FROM UserPasswords up where up.Timestamp = (SELECT min(upa Timestamp) FROM UserPasswords upa WHERE NEW.UserId = upa.UserId) AND NEW.UserId = up.UserId;
END
END;
You can retrieve the last inserted id when your first row is inserted, and store it in a variable. When 10 rows are inserted, delete the row having id < id of the first inserted record. Please try it.
first of all insert all values using your insert query
and then run this query
delete from table_name where (cond) order by id desc limit 10
must specify an id or time in one column