db.Prepare("INSERT INTO ? VALUES ()") not working [duplicate] - mysql

this is my problem: I want to check rows in a table which name is parameterized, something like table_X. The value of X comes from another table, so for example in my main table I have a column c_id and a column X, the table to join has name table_X, it EXISTS with no doubt, and it has the same column c_id, which I shall join on, to check if there are values of c_id in that table.
I've tried a view, but without success, because I can't put a parameterized table name in a view. I can parameterize where clauses and other things, but no table names.
I've tried a procedure, with
SET #q = CONCAT('select blabla from table_', X);
PREPARE stmt FROM #q;
EXECUTE stmt;
but procedures can't return values, and I need it, because I need to know if there is the c_id value in the parameterized table, else it is useless.
I've tried a function, but "Dynamic SQL is not allowed in stored function or trigger"
So what can I do to extract this data? I'm calling this view/function/whatever from PHP, and I know I can do it from PHP side, with two queries, but I need to do it db-side, for future implementations.
Is it possible?
NOTE: I can't modify the structure of the DB :) btw, it's the Limesurvey db, sounds like a crazy db structure, huh?

The only way, without dynamically building queries, is to hard code in every combination and pick out the one you want.
If the table name is a parameter to a stored procedure, this can be in IF blocks. But it feels clunky.
If the fields from each table are the same, you can union the tables together and select from those...
CREATE VIEW myUnifiedStructure AS
SELECT 'Table1' AS tableName, * FROM Table1
UNION SELECT 'Table2' AS tableName, * FROM Table2
UNION SELECT 'Table3' AS tableName, * FROM Table3
-- etc
SELECT * FROM myUnifiedStructure WHERE tableName = 'Table1'
If the fields are different in each table, you may only be interested in a subset of the fields...
CREATE VIEW myUnifiedStructure AS
SELECT 'Table1' AS tableName, field1 AS field1, field4 AS field2 FROM Table1
UNION SELECT 'Table2' AS tableName, field2 AS field1, field3 AS field2 FROM Table2
UNION SELECT 'Table3' AS tableName, field2 AS field1, field4 AS field2 FROM Table3
-- etc
Or you can pass in NULLs for fields that don't exist in the source table...
CREATE VIEW myUnifiedStructure AS
SELECT 'Table1' AS tableName, NULL AS field1, field2 AS field2 FROM Table1
UNION SELECT 'Table2' AS tableName, field1 AS field1, field2 AS field2 FROM Table2
UNION SELECT 'Table3' AS tableName, field1 AS field1, NULL AS field2 FROM Table3
-- etc

Why do you need separate tables like this? It's usually a sign of bad design. Wouldn't it just be easier to create a single table with an identifier field for whichever X` value that record belongs to, that you can join/filter on?
That'd reduce the query to something like
SELECT ...
FROM othertable
JOIN bigtable ON othertable.c_id = bigtable.c_id AND othertable.fieldName = bigtable.fieldName

Related

Create new table with calculated data from another table

I'm going to do an regression analysis through my data chunks. For that I need to find out various values. For each data set I need to get N:count(X) sumX sumY sumX*X etc.
Separately I wrote queries for those operations like
SELECT COUNT(X) FROM table_name
SELECT SUM(X*X) FROM table_name
I need to create another table which a row contain count(X), sumX , sumX*X etc. How can I write that kind of query?
You can add multiple aggregates to the same query and use create table as:
create table yournewtable as
select count(x) cnt, sum(x*x) sumxx, sum(x) sumx
from table_name
SQL Fiddle Demo
This will return you a single row. If you need to break it apart, look into group by.
CREATE TABLE first and then use INSERT INTO
CREATE TABLE yourTableName
(
col1 int,
col2 int,
col3 int
);
INSERT INTO yourTableName (col1, col2, col3)
SELECT
(SELECT COUNT(X) FROM table_name),
(SELECT SUM(X) from table_name),
(SELECT SUM(X*X) from table_name)

TSQL aggregate functions to populate temp tables

I want to run a T-SQL script where I create a temp table that will be aggregated by a certain field of another table, call it table X. The remaining fields of this temp table will be populated by performing aggregate functions on the fields of table X. Then I would like to do a MERGE / WHEN MATCHED with my temp table on a different table (call it table Y) after I have populated the temp table.
How do I create this temp table and populate it with aggregate functions? (I have already coded the MERGE part of the problem).
to create a temp table you will can do the following:
create table #temp
(
id int,
col1 int
)
then you will write an INSERT INTO
INSERT INTO #temp
SELECT col1, sum(col2)
FROM yourTable
Once you have created your temp table you can use it in your store procedure.
What bluefeet posted, or:
with rsAggregated as
(
select id, sum(x)
from tableX
group by id
)
merge...
Or, not seeing your merge statement, just
merge tableY using
(
select id, sum(x)
from tableX
group by id
) rsAggregated
on rsAggregated.id = tableY.id
when matched
...
when not matched
...
You can do the whole thing with one SELECT statement
SELECT col1 AS ID, sum(col2) AS col1 INTO #temp FROM yourTable

How to merge multiple tables with similar names in MySQL [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I merge two MySql tables?
I want to merge multiple tables that have the same structure and make one large table. The tables have similar names, so I want to use the LIKE statement. Can anyone tell me how I can do this?
The tables are very simple, each having an ID column and a few other columns, but there are a large amount of tables, all of which have names like 'TX-xxx', where 'TX' means Texas, and 'xxx' are the counties in Texas; you know there are more than 200 counties in Texas. (In fact, I have to do this for all the states.) So I want to use the statement "LIKE 'TX-___'".
Thanks!
You would have to give more information so we know exactly what you want but you could create a view
CREATE VIEW myViewName AS
select *
from table1
union all
select * from
table2
This way it would show the information from all your tables (and can be limited so in the selects to not show everything) and when table1, table2, etc are changed the view will reflect this. You can change it at anytime and fetch from it as you would a table:
select * from myViewName
Now for grabbing from specific tables I am not sure how you can do this in mysql though I have done it in tsql. This previous question would help you so you might have something like:
-- Create temporary table of varchar(200) to store the name of the tables. Depending on how you want to go through the array maybe an id number (int).
insert into tempTableName (name)
SELECT table_name FROM information_schema.tables WHERE table_schema = 'database_name' and table_name like 'TX_%';
declare #sqlQuery varchar(max)
--Then you will want to loop through the array and build up an sql statement
-- For each loop through:
if len(#sqlQuery) = 0 begin -- first time through
set #sqlQuery = 'select col1, col2, col3 from ' + currentTableName
end else begin -- second+ time through
set #sqlQuery = 'union all select col1, col2, col3 from ' + currentTableName
end
-- after the loop add the create view. Could double check it worked by checking length = 0 again
set #sqlQuery = 'CREATE VIEW myViewName AS ' + #sqlQuery
Once the query string is built up you will execute it with
PREPARE stmt FROM #sqlQuery;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
If I understand your question correctly UNION is what you need. Something like
SELECT field1, field2
FROM (
SELECT field1, field2 from table1
UNION
SELECT field1, field2 from table2
) all_tables
WHERE all_tables.field1 like "%whatever%
Assuming they have the same columns or similar:
insert into #table
Select * from (Select * from tbl1
Union
select * from tbl2
Union
select * from tbl3)
If they don't have the same number/type of columns then you should provide us with that information.

Parameterized table name

this is my problem: I want to check rows in a table which name is parameterized, something like table_X. The value of X comes from another table, so for example in my main table I have a column c_id and a column X, the table to join has name table_X, it EXISTS with no doubt, and it has the same column c_id, which I shall join on, to check if there are values of c_id in that table.
I've tried a view, but without success, because I can't put a parameterized table name in a view. I can parameterize where clauses and other things, but no table names.
I've tried a procedure, with
SET #q = CONCAT('select blabla from table_', X);
PREPARE stmt FROM #q;
EXECUTE stmt;
but procedures can't return values, and I need it, because I need to know if there is the c_id value in the parameterized table, else it is useless.
I've tried a function, but "Dynamic SQL is not allowed in stored function or trigger"
So what can I do to extract this data? I'm calling this view/function/whatever from PHP, and I know I can do it from PHP side, with two queries, but I need to do it db-side, for future implementations.
Is it possible?
NOTE: I can't modify the structure of the DB :) btw, it's the Limesurvey db, sounds like a crazy db structure, huh?
The only way, without dynamically building queries, is to hard code in every combination and pick out the one you want.
If the table name is a parameter to a stored procedure, this can be in IF blocks. But it feels clunky.
If the fields from each table are the same, you can union the tables together and select from those...
CREATE VIEW myUnifiedStructure AS
SELECT 'Table1' AS tableName, * FROM Table1
UNION SELECT 'Table2' AS tableName, * FROM Table2
UNION SELECT 'Table3' AS tableName, * FROM Table3
-- etc
SELECT * FROM myUnifiedStructure WHERE tableName = 'Table1'
If the fields are different in each table, you may only be interested in a subset of the fields...
CREATE VIEW myUnifiedStructure AS
SELECT 'Table1' AS tableName, field1 AS field1, field4 AS field2 FROM Table1
UNION SELECT 'Table2' AS tableName, field2 AS field1, field3 AS field2 FROM Table2
UNION SELECT 'Table3' AS tableName, field2 AS field1, field4 AS field2 FROM Table3
-- etc
Or you can pass in NULLs for fields that don't exist in the source table...
CREATE VIEW myUnifiedStructure AS
SELECT 'Table1' AS tableName, NULL AS field1, field2 AS field2 FROM Table1
UNION SELECT 'Table2' AS tableName, field1 AS field1, field2 AS field2 FROM Table2
UNION SELECT 'Table3' AS tableName, field1 AS field1, NULL AS field2 FROM Table3
-- etc
Why do you need separate tables like this? It's usually a sign of bad design. Wouldn't it just be easier to create a single table with an identifier field for whichever X` value that record belongs to, that you can join/filter on?
That'd reduce the query to something like
SELECT ...
FROM othertable
JOIN bigtable ON othertable.c_id = bigtable.c_id AND othertable.fieldName = bigtable.fieldName

MySQL: Update all Columns With Values From A Separate Table

Sometimes if I want to quickly copy records from one table to another (that has the same structure) I use a query like this:
INSERT INTO table2 SELECT * FROM
table1 WHERE id = SOME_VALUE
How can I add a ON DUPLICATE KEY UPDATE to this statement? I tried this:
INSERT INTO SELECT * FROM table1 WHERE
id = 1 ON DUPLICATE KEY UPDATE SELECT
* FROM table1 WHERE id = 1
But I get an error. Is there away to accomplish the query above with out individually listing each column in the query?
P.S. Yes, I realize that it is not good practice to have multiple tables with identical structures, but sometimes you just don't get control over everything in the workplace!
The below UPDATES if there is no PK duplication and INSERTs is there is:
REPLACE INTO table2(field1, field2, field3)
SELECT field1, field2,field3 FROM table1
WHERE id=1;
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html Just use the SELECT field_name from the other table like in dnagirls example