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,','),'')
)
)
)
Related
This question already has an answer here:
Select values from a list that are not in a table
(1 answer)
Closed 2 years ago.
I have a list of values:
('WEQ7EW', 'QWE7YB', 'FRERH4', 'FEY4B', .....)
and the dist table with a dist_name column.
and I need to create SQL query which would return values from the list which don't exist in the dist_name column.
Yo need to use left join. This requires creating a derived table with the values you care about. Here is typical syntax:
select v.val
from (values ('WEQ7EW'), ('QWE7YB'), ('FRERH4'), ('FEY4B')
) v(val) left join
t
on t.col = v.val
where t.col is null;
Not all databases support the values() table constructor but allow allow some method for creating a derived table. In MySQL, this looks like:
select v.val
from (select'WEQ7EW' as val union all
select 'QWE7YB' as val union all
select 'FRERH4' as val union all
select 'FEY4B' as val
) v(val) left join
t
on t.col = v.val
where t.col is null;
You would typically put this list of values in a derived table, and then use not exists. In MySQL:
select v.dist_name
from (
select 'WEQ7EW' as dist_name
union all select 'QWE7YB'
union all ...
) v
where not exists (select 1 from dist d where d.dist_name = v.dist_name)
Or if you are running a very recent version (8.0.19 or higher), you can use the VALUES ROW() syntax:
select v.dist_name
from (values row('WEQ7EW'), row('QWE7YB'), ...) v(dist_name)
where not exists (select 1 from dist d where d.dist_name = v.dist_name)
SELECT TRIM(TRAILING ',' FROM result) result
FROM ( SELECT #tmp:=REPLACE(#tmp, CONCAT(words.word, ','), '') result
FROM words, (SELECT #tmp:='WEQ7EW,QWE7YB,FRERH4,FEY4B,') arg
) perform
ORDER BY LENGTH(result) LIMIT 1;
fiddle
The list of values to be cleared from existing values is provided as CSV string with final comma and without spaces before/after commas ('WEQ7EW,QWE7YB,FRERH4,FEY4B,' in shown code).
If CSV contains duplicate values all of them will be removed whereas non-removed duplicates won't be compacted. The relative arrangement of the values will stay unchanged.
Remember that this query performs full table scan, so it is not applicable to huge tables because it will be slow.
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'
I saw recently (can't find it now) this syntax:
... LIKE CONCAT('%',col1,'%')
It is working for Selects but for update, it affects 0 rows
this is my query:
update locations set email = (
select col2 from vendoremail
where locations.city LIKE CONCAT('%',col1,'%')
AND locations.zip LIKE CONCAT('%',col1,'%')
)
here is a sample of col1 :
"455 N Cherokee St: Muskogee, OK 74403"
without the quotes
I hope I have given enough data to elicit an answer or two - thank you!
You have it backwards. You want to put the city and zip into the pattern.
update locations set email = (
select col2 from vendoremail
where col1 LIKE CONCAT('%', locations.city, '%', locations.zip, '%')
)
However, this may not always work properly. If you have two vendors in the same city+zip, the subquery will return 2 emails, but when you use a subquery as a value it has to return only 1 row. You can add LIMIT 1 to the subquery to prevent an error when this happens. But it will be selecting one of the vendors unpredictably -- maybe you should come up with a more reliable way to match the tables.
If col1 is = "455 N Cherokee St: Muskogee, OK 74403"
i think location.city is = Muskogee and locations.zip is = 74403
then the query should be
update locations
set email = (
select col2 from vendoremail
where col1 LIKE CONCAT('%',locations.city,'%')
AND col1 locations.zip LIKE CONCAT('%',locations.zip,'%')
)
I have a list of ids, and I want to query a mysql table for ids not present in the table.
e.g.
list_of_ids = [1,2,4]
mysql table
id
1
3
5
6
..
Query should return [2,4] because those are the ids not in the table
since we cant view ur code i can only work on asumption
Try this anyway
SELECT id FROM list_of_ids
WHERE id NOT IN (SELECT id
FROM table)
I hope this helps
There is a horrible text-based hack:
SELECT
substr(result,2,length(result)-2) AS notmatched
FROM (
SELECT
#set:=replace(#set,concat(',',id,','),',') AS result
FROM (
select #set:=concat(',',
'1,2,4' -- your list here
,',')
) AS setinit,
tablename --Your tablename here
) AS innerview
ORDER BY LENGTH(result)
LIMIT 1;
If you represent your ids as a derived table, then you can do this directly in SQL:
select list.val
from (select 1 as val union all
select 2 union all
select 4
) list left outer join
t
on t.id = list.val
where t.id is null;
SQL doesn't really have a "list" type, so your question is ambiguous. If you mean a comma separated string, then a text hack might work. If you mean a table, then something like this might work. If you are constructing the SQL statement, I would advise you to go down this route, because it should be more efficient.
I want to write mysql when statement and i can't do it.
explanation => localParty is column in table "data", loc is column from table "o", and i want to compare localParty to loc, if their values are equal then i want to retrieve information from loc_m column (this column is from table "o"), and if not equal then from localParty column (from "data" table)
Please help how to write this script in mysql query ? Thanks
with this script
select (case when data.localparty = o.loc then o.loc_m else data.localparty end)
as customdata from data, o
it is working but it is missing exactly three result ( I mean that then data.localparty equal to o.loca it is giving result from data.localparty 3 times and after it one time it is giving result from loc_m and it is going like so .
You could modify the query in the following way:
SELECT IF(t1.Column1 = t2.Column2,t2.Column1,t1.Column3) FROM TABLE1 AS t1, Table2 AS t2
Try This:
select (case when data.localparty = o.loc then o.loc_m else data.localparty end)
as customdata from data, o
you can use following query
Select O.loc_m as local
from Data
inner join on O on data.localparty=O.loc
UNION
Select data.loacalparty as local
from Data
where data.localparty is not in (select loc from O )
You should use control-flow functions to achieve that goal:
SELECT IF(Column1 = Column2,Column1,Column3) FROM TABLE1