selecting multiple tables in mysql and combine the results [duplicate] - mysql

The table names in my mysql database are dynamically generated. Is there some way to select data from tables which have a name matching a pattern? I guess it will look like:
select * from 'table_id_%'

No, you can't do that with MySQL. Tables in a query can't be dynamically specified - you have to build the list in your application (or do several single-table queries).

You can use INFORMATION_SCHEMA TABLES table to find tables you want, here is documentation: http://dev.mysql.com/doc/refman/5.0/en/tables-table.html . TABLES table has column NAME which represents names of tables. After finding table names you can run any sql queries you like.

That's not possible in the way you'd like to do it. However you could probably use prepared statements which are basically query-templates where you specificy the parameters (AFAIK also table names) that get replaced depending on your needs without copy and pasting the same query over and over again for different tables.

Related

How to query against multiple databases on the same server

I am not sure if this has been answered before but what I found i am not sure how to make work for me but here is my problem.
I have a database used to keep track of phones for multiple clients. What needs to be done is have a query that can be ran that will run against multiple databases on the same server. each database uses the same table name that I am looking at but the names are slightly different. I came up with this..
INSERT INTO `export db`.exportinfo2
SELECT *
FROM (SELECT * FROM `export db'.tentantnames).users
WHERE name = 'Caller ID:emergency' AND value > 0
What suppose to happen is from a table that has all the database names is is to got to each database and go into the table labeled users and run a where clause on the data then export results to a different database table
I know the code needs to be dynamic but I am not sure how to make it dynamic and function. The table that has all the names for the databases is automatically created every few days.. I am not sure what else needs to be said without sounding like i repeat myself but i just need help making a dynamic query that uses a table premade as database names and run a where statement on the same named table in each database which have their name stored in a different table.
You should look into Synonyms. It can be used to fulfill your purpose

Access query runs on table that does not exist in database

I would like to recreate a few existing Access queries in a new database so that I can tweak them a bit. The problem I am running into is that some of the tables being queried do not seem to exist in the current database. These tables all end with a 1.
For example, INV_MTL_ITEM_LOCATIONS is an imported table in the database, but
INV_MTL_ITEM_LOCATIONS_1 is being queried even though it does not show up in the tables panel on the left.
Is this some type of duplication functionality that I am not aware of? The query runs without any errors.
No, the query runs on an aliased table. It's actually just querying INV_MTL_ITEM_LOCATIONS
Using SQL, you can create an alias for a table. This is especially useful when querying the same table twice in one query, but also commonly used to shorten queries.
Your query will probably look something like this:
SELECT something
FROM INV_MTL_ITEM_LOCATIONS AS INV_MTL_ITEM_LOCATIONS_1
Access automatically creates these aliases when using the query builder and if you add the same table more than once. When removing the non-aliased table, the other one stays aliased.
This is entirely normal, and as far as I know, never a problem.
Erik's answer explains it perfectly.
But to be exhaustive, you CAN actually create a query on a table that is NOT in the current database (nor a linked table).
Here is an example:
SELECT *
FROM History IN 'c:\test\mySecretBackend.accdb'
You can also create that in design view, just by
setting the query's Source Database property to c:\test\mySecretBackend.accdb
click on Show table

Batch replacements in MySQL Database

I've learned how to do simple search and replace operations in my MySQL databases using phpMyAdmin...
UPDATE my_table SET my_column = replace(my_column,'Spain','Spanish')
I just wondered if there are ways to target more than one fields and/or tables at a time. For example, if you have three tables named One, Two and Three and each one has a field named Article, could you do a search-and-replace operation in all three tables simultaneously? Or could you even search fields with different names, like One.Article, Two.Article, Three.Content?
Solutions don't have to be limited to phpMyAdmin. I'm interested in learning about other popular DB administration programs.
Yes, UPDATE supports multi-table syntax:
UPDATE One, Two, Three
SET One.Article = replace(One.Article,'Spain','Spanish'),
Two.Article=replace(Two.Article,'Spain','Spanish'),
Three.Content=replace(Three.Content,'Spain','Spanish');

How to compare two databases' altered tables

I have inserted new columns in one database, and I now want to add the same type of columns to a second database. I need to know which columns are in the first database that are not in the second. I have many tables in each database that needs column difference comparing. I searched the web and I can only find ways to see the difference of the contents of columns in two tables. I don't need to compare the contents, just different columns in all the tables in each database. Each database has the same tables.
Thanks!
I found that you can do a database dump that just has the structure from phpmyadmin.
Are you doing this manually? You could just use SHOW CREATE to see the structure of the tables, and then something like the diff command in Linux to compare them.
For a commercial product answer: I use Red Gate's SQL Compare which works great. It can compare the entire schema of two databases. It can also update your target database to match your source database.
Use redgate SQL compare to comapre schema of two tables.
sql-dbdiff works well too. Its an open source.

Use of wildcards in mysql table name

The table names in my mysql database are dynamically generated. Is there some way to select data from tables which have a name matching a pattern? I guess it will look like:
select * from 'table_id_%'
No, you can't do that with MySQL. Tables in a query can't be dynamically specified - you have to build the list in your application (or do several single-table queries).
You can use INFORMATION_SCHEMA TABLES table to find tables you want, here is documentation: http://dev.mysql.com/doc/refman/5.0/en/tables-table.html . TABLES table has column NAME which represents names of tables. After finding table names you can run any sql queries you like.
That's not possible in the way you'd like to do it. However you could probably use prepared statements which are basically query-templates where you specificy the parameters (AFAIK also table names) that get replaced depending on your needs without copy and pasting the same query over and over again for different tables.