sql -- select and update - mysql

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.

Related

MySQL: Update table with an IN (#variable) failed

I'm trying to update a table, with the IN function and a variable.
The content of the variable is a sub-query that returns the expected values aka ID for each STRING. When I copy/paste the values in the update, everything is fine.
USE `DB1`;
SET SQL_SAFE_UPDATES = 0;
SET #VAR1 = "STRING1,STRING2,STRING3,STRING4";
SET #VAR2 = (SELECT GROUP_CONCAT(`id`) FROM `tbl_A` WHERE FIND_IN_SET(`description`, #VAR1) AND `fieldtype` = '');
UPDATE `tbl_A`
SET `idaccount` = 2
WHERE `id` IN (#VAR2);
SET SQL_SAFE_UPDATES = 1;
So why when I use the variable, it updates only the first row?
The variable #VAR2 is a scalar variable, and can't store a result set. To do that, you would need a table variable, which MySQL does not directly support (note that other databases, such as SQL Server, do support them).
But, you may inline the subquery and it should work:
SET #VAR1 = "STRING1,STRING2,STRING3,STRING4";
UPDATE tbl_A
SET idaccount = 2
WHERE id IN (SELECT id FROM (
SELECT id FROM tbl_A
WHERE FIND_IN_SET(description, #VAR1) AND fieldtype = ''
)x );
Note that the subquery following the IN clause of the update is wrapped twice, to avoid the problem of trying to reference the same table which is being updated. This trick actually materializes the subquery, leaving it independent of what happens during the update.

UPDATE (B) from (A) if (B) = 0?

I'm learning this for school and I'm confused. I'm trying to copy information between columns in a single table in a single db, all local.
Basically:
(I need to loop through and update all records)
UPDATE `my_records`
SET `realname` = `name`
WHERE `realname` = 0;
SELECT * FROM `my_records` SET `realname` = `name` WHERE `realname` = 0;
It keeps telling me I have a syntax error.
I now see why they are asking me to learn this. Each row in the table is different so when I update all columns some rows change that shouldn't so that's not the end result I'm after. I can try to give an example but this is confusing to me.
DB -> Table -> Row 1 - holds the name of the person -> Row 2 - holds the picture
of the person
Both things have a name (example Row 1 David, Row 2 Flower.JPG)
So I'm guessing they want me to figure out a way to exclude updating the 'real_name' column on Row 2 where the image is a JPG, GIF, or PNG.
I think the final result they are looking for when the table is updated is:
Row 1 'David' 'David'
Row 2 'flower.jpg'
Then this loops over and over again for all the records.
You need to use an UPDATE instead of a SELECT. SELECT statements only return data, they do not modify data.
So, to return the records you will update in the next step:
SELECT `realname`, `name` FROM `my_records` WHERE `realname` = 0;
and then to update those records:
UPDATE `my_records` SET `realname` = `name` WHERE `realname` = 0;
Note that this query will update the entire table, setting any row where the value of realname is equivalent to 0, to the value of that same row's name column.
A few other possibly useful statements:
UPDATE `my_records` SET `realname` = `name` WHERE `realname` = '';
This will affect all rows where realname is equivalent to 'empty string'
UPDATE `my_records` SET `realname` = `name` WHERE `realname` IS NULL;
will affect all rows where realname is NULL

if value is not empty, then insert mysql

I have a table in mysql like this (the id is primary key):
id | name | age
1 | John | 46
2 | | 56
3 | Jane | 25
Now I want to update the name only if this is empty. If the value is not empty it should duplicate the row with a new id else it should update the name.
I thought it could be done with an if-statement but it doesn't work.
if((select `name` from `table1` where `id` = 3) = '',
update `table1` set `name`='ally' where `id` = 3,
insert into `table1` (`id`,`name`,`age`) values
(4, 'ally', select `age` from `table1` where `id` = 3))
EDIT:
With Spencers answer I made it working using an if in the code. (However I would still like a way to do just a single mysql query).
db.set_database('database1')
cursor = db.cursor()
query = "select IF(CHAR_LENGTH(name)>0,1,0) from table1 where id = {0}".format(id)
cursor.execute(query)
val1 = cursor.fetchone()
if val1[0]:
query = "INSERT INTO `table1` (`id`,`name`,`age`) SELECT {0},{1},`age` FROM `table1` WHERE `id` = {2}".format(new_id, name, id)
cursor.execute(query)
else:
query = "update `table1` set `name` = '{0}' where `id` = {1}".format(name, id)
cursor.execute(query)
db.commit()
If you make like this :
select t.*,
if(
EXISTS(select n.name from table1 n where n.id = 2 and NULLIF(n.name, '') is null) ,
'true',
'false'
) from table1 t
if statement returns "true", becouse in your table exist row where id =2 and name is empty.
like this example, You can edit your query :
if(
EXISTS(select n.name from table1 n where n.id = 3 and NULLIF(n.name, '') is null),
update `table1` set `name`='ally' where `id` = 3,
insert into `table1` (`id`,`name`,`age`) values
(4, 'ally', select `age` from `table1` where `id` = 3)
)
IF is not a valid MySQL statement (outside the context of a MySQL stored program).
To perform this operation, you'll need two statements.
Assuming that a zero length string and a NULL value are both conditions you'd consider as "empty"...
To conditionally attempt an update of the name field, you could do something like this:
UPDATE table1 t
SET t.name = IF(CHAR_LENGTH(t.name)>0,t.name,'ally')
WHERE t.id = 3 ;
The IF expression tests whether the current value of the column is "not empty". If it's not empty, the expression returns the current value of the column, resulting in "no update" to the value. If the column is empty, then the expression returns 'ally'.
And you'd need a separate statement to attempt an INSERT:
EDIT
This isn't right, not after a successful UPDATE... of the existing row. The attempt to INSERT might need to run first,
INSERT INTO table1 (id,name,age)
SELECT 4 AS id, 'ally' AS name, t.age
FROM table1 t
WHERE t.id = 3
AND CHAR_LENGTH(t.name)>0;
We need a conditional test in the WHERE clause that prevents a row from being returned if we don't need to insert a row. We don't need to insert a row if the value 'ally' ...
The use of CHAR_LENGTH >0 is a convenient test for string that is not null and is not zero length. You could use different test, for however you define "empty". Is a single space in the column also considered "empty"?)

How to get rows whose columns values are not null

I want to get rows of a table such that no column value is null. No hardcoding of column values. I have hundreds of column names so.
Output should be only row 2 since all that row has the values for all the columns. I do not want to specify all the column names for is not null. It should take it programmatically. Even if i add a new column it should work without changing the query. That is my vision.
I found something, but that means using CURSOR
DECLARE #ColumnName VARCHAR(200)
DECLARE #ColumnCount INT
DECLARE #sql VARCHAR(400)
CREATE TABLE #tempTable (Id INT)
DECLARE GetNonNullRows CURSOR
FOR
SELECT c.NAME, (SELECT COUNT(*) FROM sys.columns col WHERE col.object_id = c.OBJECT_ID) FROM sys.tables AS t
JOIN sys.columns AS c ON t.object_id = c.object_id
WHERE t.name = 'SomeTable' AND t.type = 'U'
OPEN GetNonNullRows
FETCH NEXT FROM GetNonNullRows INTO #ColumnName, #ColumnCount
WHILE ##FETCH_STATUS = 0
BEGIN
SET #sql = 'SELECT st.UniqueId FROM SomeTable AS st WHERE ' + CONVERT(varchar, #ColumnName) + ' IS NOT NULL'
INSERT INTO #tempTable
EXEC (#sql)
FETCH NEXT FROM GetNonNullRows INTO #ColumnName, #ColumnCount
END
CLOSE GetNonNullRows
DEALLOCATE GetNonNullRows
SELECT * FROM SomeTable AS st1
WHERE st1.UniqueId IN (SELECT Id FROM #tempTable AS tt
GROUP BY Id
HAVING COUNT(Id) = #ColumnCount)
DROP TABLE #tempTable
Let me to explain this a little.
First i create cursor which iterate through all the columns of one table. For each column, I've create sql script to search in table for not null values for selected column. For those rows that satisfies criteria, I take its unique ID and put in temp table, and this job I am using for all columns.
At the end only ID's which count is like columns count are your result set, because only rows that have identical number of appearances like number of columns in table may be rows with all non null values in all columns.
Try this ::
SELECT * FROM mytable WHERE column IS NOT NULL
try this
SELECT *
FROM your_table_name
where coalesce(column_1, column_2, column_3, ...., column_n) is not null
SQL alone cannot express such a concept.
You have to dinamically build the SQL query according to the table definition using some procedural language.
In Oracle you can use the dictionay view USER_TAB_COLUMNS to build the column list.
try using IS NOT NULL
SELECT * FROM table WHERE field_name IS NOT NULL
For more information, check out the mysql manual on working with null values.

UPDATE field of last entry SQL

I like to update the field "filepath" of my table "imagedata" of the last entry made?
UPDATE `imagedata` SET `filepath`='sdsd' WHERE `id` = MAX(imagedata.id);
Somehow my synax ist not right it says:
Invalid use of group function`
what do i do wrong?
UPDATE `imagedata`
SET `filepath`='sdsd'
order by id desc limit 1
Another alternative:
UPDATE `imagedata`
SET `filepath`='sdsd'
where id = (select * from (select max(id) from imagedata) as t)
If you use an AUTO_INCREMENT column, You could try
UPDATE `imagedata` SET `filepath`='sdsd' WHERE `id` = LAST_INSERT_ID();