Copy field from one table to another with REGEXP_REPLACE - mysql

Before running a REGEXP_REPLACE on a big table, I want to preview the results, so I want to copy the 'before' and 'after' of the modified field to another table so I can audit.
What's the best way to do this?
Something like
INSERT INTO table2 (before, after)
SELECT field1, REGEXP_REPLACE(field1,'foo','bar')
FROM table1
WHERE condition
(MariaDB)

If this were my project I'd do these things.
First. Just do this and eyeball the results.
SELECT COUNT(*), field1, REGEXP_REPLACE(field1,'foo','bar')
FROM table1
WHERE field1 <> REGEXP_REPLACE(field1,'foo','bar')
GROUP BY field1, REGEXP_REPLACE(field1,'foo','bar')
ORDER BY COUNT(*), field1
That will show you the least frequent values first so you can see the one-off problems caused by your replace first. No need to create a table.
Second, I'd eyeball the values that DIDN'T change with this, changing the WHERE clause from <> to =.
SELECT COUNT(*), field1
FROM table1
WHERE field1 = REGEXP_REPLACE(field1,'foo','bar')
GROUP BY field1
ORDER BY COUNT(*), field1
Maybe some stuff didn't change that should have.
Edit SQL can get a little verbose. If you're fiddling around with some complex conversion functions you might try creating a view. Something like this:
CREATE OR REPLACE VIEW testview AS
SELECT field1,
REGEXP_REPLACE(field1,'foo','bar') changed
FROM table1;
Then you can do
SELECT COUNT(*), field1, changed
FROM testview
WHERE field1 <> changed
GROUP BY field1, changed
ORDER BY COUNT(*), field1;
And similar queries. If you must change your replace function, you can edit the view definition and do the CREATE OR REPLACE again.

Related

How do I execute a query using INSERT INTO and WITH statement?

INSERT INTO tablename( columnname1, columnname2, columnaname2)
WITH
a AS
SELECT * FROM tablename
WHERE condition
),
b AS
SELECT * FROM tablename
WHERE condition
)
I have a couple lines of query below where I use an DISTINCT statement but would like to know for now whether my query above is correct or not.
WITH? This is going to be introduced in MySQL 8.0. Are you using a preview release? Otherwise you won't be able to use WITH in MySQL.
Anyway: A WITH clause belongs at the beginning of the statement: WITH ... INSERT .... See here: https://dev.mysql.com/doc/refman/8.0/en/with.html.
It seems, however, you are not even using your CTEs a and b. Your CTEs are also lacking parentheses. Your statement should look something like this for instance:
WITH a AS (SELECT * FROM tablename WHERE condition)
, b AS (SELECT * FROM tablename WHERE condition)
INSERT INTO tablename(columnname1, columnname2, columnaname2)
SELECT col1, col2, col3 FROM a
UNION ALL
SELECT col1, col2, col3 FROM b;

SQL INSERT from where clause to another where clause

How do I copy content from one table with a where clause to the same table to another where clause?
So the Select Query would be:
SELECT * FROM items WHERE countryNr=5;
I want now to insert all data from the above query into for example countryNr=1.
So countryNr=5 and CountryNr=1 should habe in the end the same data.
What you have described is more like an UPDATE than INSERT...
in that case your query could be something like this:
update item
set (field1, field2) = (select field1, field2 from item where countrNR = 5)
where countrNR = 1
I'm not sure I understand the question but something like... mySQL docs for insert into select
Insert into items Select Field1, Field2, Field3, Field4..., 1 as countryNR
from items where countrNR = 5

MySQL insert into (conditionally selected table) in single statement

My dbm knowledge is still pretty limited, so I am not sure how to approach/solve this problem. I want to INSERT INTO one of two tables, say, table1 and table2, but I don't know which table until after a SELECT subquery. Something like this:
INSERT INTO (SELECT tblname) SELECT *, IF(somecondition, 'table1', 'table2') as tblname FROM `anothertable` WHERE id = 'someid'
I tried this as a test:
INSERT INTO (SELECT tblname) SELECT *, 'table1' as tblname FROM `anothertable` WHERE id = 'someid'
But that didn't work.
I know I can use subqueries in SELECT statements (so useful!), and that I can technically achieve what I want with NOT EXISTS in 2 statements, and I know I cannot INSERT into two tables, and I know that using # user variables is unreliable within a statement (see docs). So, is there a way to achieve what I want, in a single statement?
you can do it with case statement.
SELECT CASE WHEN ( SELECT IF(somecondition, 'table1', 'table2') as tblname FROM `anothertable` = 'table1' )
THEN <QUERY A>
ELSE <QUERY B>
END

field constant using laravel query builder

Using laravel/fluent query builder, I'm trying to cause a constant field value to pass through for a union(ed) selection that is subsequently ordered . I haven't found the recipe to do the following with fluent. The unions are easy, but how do you get the field constant to work?
Imagine two simple tables (omitted) and a union select:
select field1, field2, 'type1' as field3 from table1
UNION
select field1, field2, 'type2' as field3 from table2
ORDER BY field2
The best answer I've come up with so far, is to use a DB::query with a query string I manufacture myself. Laravel/fluent does not seem ready to handle this case, given the test cases I've tried. Using RAW for a select works great, until you try to order the pair of selected table queries.
SELECT field1, field2 FROM
(
SELECT fld1A as field1, 'FOO' as field2 from table1
UNION ALL
SELECT fld2A as field1, 'BAR' as field2 from table2
)
temp_table order by somefield
Using Laravel 4, and using GROUP BY, rather than ORDER BY I believe you can do something like:
$t1 = DB::table('table1')
->select('field1',DB::raw("'FOO' as field2"))
->groupBy('field2');
$t2 = DB::table('table2')
->select('field1',DB::raw("'BAR' as field2"))
->groupBy('field2');
$result = $t1->union($t2)->get();
I found that $t1 in this case can be an instance of Illuminate\Database\Query\Builder or Illuminate\Database\Eloquent\Builder, but the union argument ($t2) must be of type Illuminate\Database\Query\Builder.
This means that you may use eager loading with something like:
$t1 = MyTableModel::with('table3')->select...
This way, probably:
$users = DB::table('users')
->select(DB::raw("'FOO' as field2"))
->get();

MySQL ORDER BY logic

What's the logic?
SELECT field1, field2,...fieldN table_name1, table_name2...
ORDER BY field1, [field2...] [ASC [DESC]]
So if I have ORDER BY field1, field2 :
(Please let me know if i'm wrong) it will sort results by field1 and if field1 has the same values in a couple rows (field1[235]=field1[236]) only in then ORDER BY field2 kicks in. Right?
Indeed.
That is how it works.
But why do I see so many of these questions here on SO? What happened to experimenting, trying stuff out, playing, etc?