MySql query for each wordpress multisite - mysql

I'm attempting a query that pulls information for each site in a Wordpress multisite environment. Here's what I have for reference:
SELECT
#optionTable:=CONCAT('p1m8z_', blog_id, '_options') AS optionTable,
(SELECT option_value FROM #optionTable WHERE option_name = 'blogname') AS blogName
FROM p1m8z_blogs
Though this code doesn't execute, it illustrates what I'm attempting to accomplish.
I know that I can't use a variable as a table name unless it's in a prepared statement so I tried slapping a prepared statement into the subquery:
SELECT
#optionTable = CONCAT('p1m8z_', blog_id, '_options') AS optionTable,
(
PREPARE stmt1 FROM "SELECT option_value FROM ? WHERE option_name = 'blogname'";
EXECUTE stmt1 USING #optionTable;
DEALLOCATE PREPARE stmt1;
) AS blogName
FROM p1m8z_blogs
That doesn't seem to execute either. Can I even put a prepared statement inside a subquery?
How can I write this statement so that it executes?

Both variables and parameters are treated as if they are string values. You cannot use a variable or a parameter as an identifier (i.e. a table name).
You must just concatenate any table names into the string before it is prepared.
SELECT CONCAT('p1m8z_', blog_id, '_options') INTO #optionTable
FROM p1m8z_blogs;
SET #query = CONCAT('SELECT option_value FROM `', #optionTable,
'` WHERE option_name = ''blogname''');
PREPARE stmt1 FROM #query;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
The syntax you showed where you embed the PREPARE/EXECUTE inside another query as if it's a subquery is not valid. Only SELECT can be used inside a subquery.

Related

Creating and then using a database with a declared variable name

I'm trying to create a database named 'uppg' and then use that database once it's created.
The first statement executes as it should, but nothing happens when I try to use the database with the variable. I have tried multiple variations but I don't know why it's not working.
SET #dbname = 'uppg';
SET #q1 = CONCAT(
'CREATE DATABASE `', #dbname, '`');
PREPARE stmt1 FROM #q1;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
SET #q2 = CONCAT(
'USE ', #dbname,
);
PREPARE stmt2 FROM #q2;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;
There exists complete list of statements allowed in PREPARE: SQL Syntax Permitted in Prepared Statements.
USE is not mentioned in it. So you cannot alter current database using dynamic SQL.
The simplest solution: use complete tables names (including database name) in your queries. In this case the current DB can be any (but not none) - this won't effect your queries.

How to put table name as variable in select query without using prepare statement in mysql?

My code is working with this prepare statement but I don't want to use prepare statement... Please give me another solution
set #sql1 = concat('select *from ', #table_names,'');
PREPARE stmt1 from #sql1;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
Thanks to all
Identifiers (e.g. table names) must be fixed in the query before the query is parsed.
So you cannot use as a table name:
a variable
a parameter
a string expression
result of a subquery
etc.
You may only do what you're doing — write the new query as a string, and include the dynamic parts as a variable or an expression. Then you have a string, and you can prepare & execute that string. The parser has no way of knowing which parts of that string were originally a variable or an expression.

Table in FROM clause defined by variable in MySQL

I want to create a SQL script for MySQL 5.7 that inserts data from a table of a database origin into a table of another target database.
I want to have this source-database defined by a variable.
USE my_target_db;
SET #origin_db='my_origin_db';
SET #origin_table = concat(#origin_db,'.','tablename');
INSERT INTO target_table SELECT * FROM #origin_table;
Variables are used in various example to define column names but I never seen a way to define a table with it.
Is anyone has a trick for this ?
Variables won't use in table name in MySQL. You only can use a prepared statement for dynamic build query. For example:
USE my_target_db;
SET #origin_db='my_origin_db';
SET #origin_table = CONCAT(#origin_db,'.','tablename');
SET #query = CONCAT('INSERT INTO target_table SELECT * FROM ', #origin_table);
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
You can read more detail about it in official documentation
You can use Prepared Statement like this:
USE my_target_db;
SET #origin_db='my_origin_db';
SET #origin_table = concat(#origin_db,'.','tablename');
SET #qry1 = concat('INSERT INTO target_table SELECT * FROM ', #origin_table);
PREPARE stmt1 from #qry1;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;

How can i add dynamic column in table exists with mysql?

i have table and i want to add column but the name of column will be variable
like this :
$coulName = col_1_2;
ALTER TABLE `table name` ADD `$coulmName` DOUBLE NOT NULL DEFAULT '0' AFTER `col2`;
how can i do that ?
You need to use a prepared statement for this:
SET #colName = 'col_1_2';
SET #s = CONCAT('ALTER TABLE `mytable` ADD `', #colName,
'` DOUBLE NOT NULL DEFAULT 0 AFTER `col2`');
PREPARE stmt1 FROM #s;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
Within MySQL you can only achieve this using prepared statements because you cannot tie a variable to a table or column name. This means that you have to assemble the sql statement in a string and execute it.
However, you can accomplish this from your application code as well - the variable name suggests that you may use php. The same applies: you have to concatenate the sql statement string, cannot use parameters.
Code would look sg like the below in MySQL:
#coulName = 'col_1_2';
#sql = 'ALTER TABLE `table name` ADD `',#coulmName,'` DOUBLE NOT NULL DEFAULT '0' AFTER `col2`;
PREPARE stmt1 FROM #sql;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
However, I'm not sure if it a really good idea to regularly and dynamically change the existing data structure. That usually indicates poor database design.

Is there a easy way to rename a table in procedure?

I am using MariaDB 10.1 SQLyog 11.5
I've renamed the table using below query.
ALTER TABLE old_name RENAME new_name
But there are many stored procedures that reference 'old_name'. I've opened all the SP creation query and changed. Because I don't know which SP has a query that reference old_name table.
Is there a way to know which SP has queries that reference old_name table?
By this INFORMATION_SCHEMA.ROUTINES you can fetch the text available in the stored procedure
Please could try this query: (not verified)
SELECT ROUTINE_NAME
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%old_name%'
-- AND ROUTINE_TYPE = "PROCEDURE" -- Filter only if need SPs
ORDER BY ROUTINE_NAME;
Reference from this page
You can it do like this:
Create RENAME Query
SELECT CONCAT ('RENAME TABLE ',
GROUP_CONCAT(t.TABLE_SCHEMA,'.',t.TABLE_NAME,' TO ', t.TABLE_SCHEMA,'.',REPLACE(t.TABLE_NAME,'old','NEW') SEPARATOR ' , ')
) INTO #sql
FROM information_schema.TABLES t
WHERE t.TABLE_TYPE = 'BASE TABLE'
AND t.TABLE_SCHEMA IN ('SCHEMA1','SCHEMA2')
AND t.TABLE_NAME LIKE 'old_table%';
ONLY to verify
SELECT #sql;
Prepare and execute it
PREPARE stmt FROM #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;