Using MySQL 5.5, no need to upgrade since it's on an old server and only used for Grafana.
I have two tables, foo, and bar. Foo has id and the title is null. I want to get the titles from bar and insert them in foo.
The only problem is that the IDs in bar have the same id in foo but are modified like so "uv10000000 + video_id" and the id in foo has a string length >= 10.
Let's say foo has an id 85, the join should be done on bar on id uv10000085. I should do the following:
- Check if the length of an id in bar >= 10
- If it is, then I should remove the uv
- And then subtract the 10000000
- And only then do the join on foo.
Here's an SQL Fiddle, and here's the query that I would like to do
UPDATE foo as f join bar as b on b.id = f.id
set b.title = f.title
JOIN
predicate, you risk the MySQL Engine coming up with an inefficient query plan. Depending on how much data is in your tables, your query may perform poorly. If you weren't on such an old version of MySQL you'd be able to add a generated column to your table that persists the results of the expression for you, but that's not possible in MySQL 5.5. Still, you'd be better off adding a column to yourbar
table where you store the cleaned up version of theID
, and onINSERT
manually maintain it with application code, or with a trigger.