MySQL - Using update/set with like from multiple tables - mysql

I am trying to update one column called 'Name' in one table using the naming conventions from another table.
My script below is not working and I am not quite sure why... Either get a syntax error or another way I tried to do this I ended up with all the 'Name' as NULL:
UPDATE table1
SET Name =
ISNULL(
(SELECT TOP 1 CorrectSSPname
FROM table2
WHERE UPPER(Name) LIKE '%’ + UPPER(WrongSSPname) + ‘%')
, Name
)
WHERE DATE >= '2018-07-01'
I can get 'like' to work using the following script for a single update but unable to do multi updates using the script above:
UPDATE table1
SET Name = 'xxx'
WHERE Name like 'yyy'

You can try a query like this:
UPDATE tabel1 tbl1
LEFT JOIN table2 tbl2 ON UPPER(tbl2.WrongSSPname) LIKE CONCAT('%', UPPER(tbl1.Name), '%')
SET tbl1.Name = tbl2.CorrectSSPname
WHERE tbl1.DATE >= '2018-07-01'

Related

update query with replace and concat

Am trying to execute below query but its not working as expected.
Please check sqlfiddle to get details about question.
Query :
update entity_permission
set permissions = REPLACE(permissions,
concat(",",
select id
from menus
where url='user_rate_list'
),
'')
where id=0;
Explanation :
A table1 have field1 in which values like 1,2,3,4,5,7,8 and I would like to replace it with the blank on some places. I know right now problem with concat is not working with a subquery. but I think it must be working some another way.
So is it possible to achieve using a single query?
Share some good suggestion.
You can use Group_concat() function to get id(s) in a comma separated string from menus table where url = 'user_rate_list'.
Now, Cross join this Derived Table with entity_permission
table.
Utilize Replace() function to update the values.
Try the following:
UPDATE entity_permission AS ep
CROSS JOIN (SELECT CONCAT(',',GROUP_CONCAT(id)) AS ids
FROM menus
WHERE url = 'user_rate_list') AS m2
SET ep.permissions = REPLACE(ep.permissions, m2.ids, '')
WHERE ep.id = 0
try this query :
update table1 set field1 = REPLACE(field1,(select GROUP_CONCAT(id SEPARATOR ',') from table2 where module_url='project1/user_list'),'') where type=0;
Edit answer :
assume that :
a = (select GROUP_CONCAT(id SEPARATOR ',') from table2 where module_url='project1/user_list')
then you can use if condition, something like below which covers 4 conditions:
update table1 set
field1 = if(field1 like concat('%,', a, ',%'),
REPLACE(field1,concat(',',a,','),''),
if(field1 like concat('%,', a),
REPLACE(field1,concat(',',a),''),
if(field1 like concat(a, ',%'),
REPLACE(field1,concat(a,','),''),
REPLACE(field1,concat(a,','),'')
)
)
)

mySQL update a value

Modified some stuff from my pic so you guys can understand it
I have this database. I am trying to update a value from a table based on another value from an another table.
I want to update the SUM from salary like this :
( sum = presence * 5 )
This is what I've been trying to use ( unsuccessful )
update table salary
set suma.salary = users.presence * 5
FROM salary INNER JOIN users1 INNER JOIN presence on id_salary = id_presence
I am not sure what to do, I'd appreciate some help, Thanks
In MySQL to UPDATE tables with a join you use this syntax:
UPDATE table1, table2
SET table1.column = some expression
WHERE table1.column = table2.column
That said, even with the updated picture, in your SQL you are mentioning columns that I cannot understand in which table are to be found. You also have an inner join between salariu and users1, with no join condition. Could you please clean up the question and make everything clear?
Assuming you are making the updates to the db structure you were talking about, then you can start working on this one maybe:
UPDATE salary, presence
SET salary.sum = SUM(presence.hours) * 5
WHERE presence.id = salary.id
AND <some filter on the month that depends on salary.date>
Another way, but I'm not sure it is supported in all RDBMS, would be something like this:
UPDATE salary
SET sum = (
SELECT SUM(presence.hours) * 5
FROM user, presence
WHERE presence.id = salary.id
AND <some filter on the month that depends on salary.date>
)

Updating a table with value sum of another table in VBA

I would like to update a sum(columne) from table1 to column from table2
I try to do this in sql like this:
UPDATE stock SET stock.chairout = (SELECT SUM(chairs_count) FROM Event
)
WHERE (([Event].[returned]))=False;
but it's giving me error like (it's not update able query)
can you guys help out on this?
Your query is:
UPDATE stock
SET stock.chairout =
(
SELECT SUM(chairs_count)
FROM Event
)
WHERE Event.returned = False
Shouldn't your where clause be on the inside of the parenthesis, like so:
UPDATE stock
SET stock.chairout =
(
SELECT SUM(chairs_count)
FROM Event
WHERE Event.returned = False
)

Update new table with specific select from other table MySql

This one is braking my head . Moving old articles rating for client from old DB to new DB where the only reference between them is first word which is separated by dash in article title. I am able to pull the info I need by using SELECT but I cant figure out how to use the result to update the new table
table that needs to be updated
UPDATE
newDB.newtable.rating
SET newDB.newtable.rating.rating_count = oldvotes
the select that gives me the info on oldvotes
SELECT
oldvotes.votes AS oldvotes, old.title AS oldtitle,newtable.news_items.title as newtitle,newtable.news_items.id AS newID
FROM
oldDB.news_items AS old
INNER JOIN
oldDB.news_items.rating_count AS oldvotes
ON
oldvotes.article_id = old.id
INNER JOIN
newDB.newtable.news_items
ON
newDB.newtable.news_items.title
LIKE CONCAT
( '%', SUBSTRING_INDEX( old.title, '- ', 1 ) , '%' )
any help is appreciated!
If I understand correctly, you have in old.title something like thisisauniquekey-september-2012, and in news_items.title the value 'thisisauniquekey-somethingelse'.
You could select a key (a faster key than the title) and the oldvotes into a temporary table, say, oldratings, using the same query you run now:
SELECT news_items.keytobeusedonnewtable AS keyforrating, oldvotes.votes as oldvotes FROM etc.
Then you can run the update using oldratings:
UPDATE newDB.newtable.rating SET rating_value = oldvotes FROM
newDB.newtable.rating JOIN oldratings
ON rating.keyforrating = oldvotes.keyforrating;

How to run a case sensative comparison on text fields Access

I am trying to run a query to remove a set of ID's from a table when they are present in a field from another table.
The problem is both ID fields are of type text and the search does not appear to be case sensitive (but I need it to be). (i.e. ABC123 is different than abc123)
I am running a query similar to Select myID from table1 where myID NOT IN (Select otherID from table2)
What modification do I need to make in my Access query to make the results case sensitive when running comparison?
Try this:
SELECT a.*
FROM table1 a LEFT JOIN table2 b
ON a.myID = b.otherID
WHERE StrComp(IIF(IsNull(b.otherID ), a.myID , b.otherID), a.myID, 0) <> 0
OR IsNull(b.otherID)