I have multiple databases and I need to search for a value in a table and set it to NULL in all instances across all databases.
I know how to query individual databases after "USE 'DBName'" is called to select a particular database.
How do I loop all databases?
Use the SHOW DATABASES query to list all databases.
You don't need to USE the database to query its tables:
SELECT something FROM databasename.sometable
Related
I tried this query below and got a database query builder not converted to string error
ModelA::join(DB::connection('mysql2')->table('table1'), 'modela.id','table1.id')->get();
Eloquent does not support joining data from different connections.
If both databases are stored on different servers, the only thing you can do is to fetch relevant data from both connections and then join it programatically.
Joining data from different databases is possible if both databases are stored on the same server. In order to do that, you'll need to prefix table names with database names. With Eloquent, you'd additionally need to use the same connection to access both databases.
Then, it could look like this:
ModelA::join('database2.table1', 'modela.id', 'database2.table1.id')->get()
I need to fetch the records from 2 different databases using join or any other methods.
I have created different connection to connect with two databases but I don't know how I can execute the query and which connection should I used to fetch the records.
Any suggestions?
I want to loop through some databases (the count of the databases are undefined) searching for a table and than I want to make SELECT and UPDATE queries in that DB where I found that table.
You can do a select against INFORMATION_SCHEMA.TABLES to locate the table by name:
SELECT
`TABLE_SCHEMA`,
`TABLE_NAME`,
`TABLE_ROWS`
FROM `INFORMATION_SCHEMA`.`TABLES`
WHERE `TABLE_NAME` like '%_assets'
Unless the desired table names might vary (different prefix, capitalization, etc.), use = 'name' instead of like '%_assets'
There are two solutions to your problem:
1). MySQL based using Federated Engine (as mentioned by you). You can go through this Blog post on Federated Engine
2). (As you are using PHP) Fetching data from one server, then either looping through the data and firing relevant queries to another server, or firing single aggregated query to another server based on the data fetched from first server.
Hope it will help you...
To list the various tables in a database you can query the database schema for the info you want.
select `table_name` as 'name'
from `information_schema`.`tables`
where `table_type`='base table' and `table_schema`=database();
The same approach can be used to find other items in the database such as events,triggers,stored procedures etc etc
I have a table with a lot of records (could be more than 500 000 or 1 000 000).
I want to update some common columns with the same field name in all tables throughout the database.
I know the traditional way to write separate queries to individual tables but not one query to update all records of all tables.
What is the most efficient way to do this in SQL, without using some dialect-specific features, so it works everywhere (Oracle, MSSQL, MySQL, Postgres etc.)?
ADDITIONAL INFO: There are no calculated fields. There are indexes. Used generated SQL statements that update the table row by row.
(This sounds like the classic case for normalizing that 'column'.)
Anyway... No. There is no single query to locate that column across all tables, then perform an UPDATE on each of the tables.
In MySQL, you can use the table information_schema.COLUMNS to locate all the tables containing a particular named column. With such a SELECT, you can generate (using CONCAT(), etc) the desired UPDATE statements. But then, you need to manually run them (via copy and paste).
Granted, you could probably write a Stored Procedure to wrap that into a single call, but that is too risky. What if some other table has the same column name, but should not be updated?
Example of building ALTERs to change tables' Engines: http://mysql.rjweb.org/doc.php/myisam2innodb#generating_alters
Example of using an SP to "pivot" rows to columns, complete with executing the generated code: http://mysql.rjweb.org/doc.php/pivot
As for common code across multiple vendors -- forget it! Virtually every syntax needs some amount of tweaking.
I'm intending to develope a web that interacts with three different Mysql databases in the same server. User and password are the same in all three databases.
I want to use mysqli in procedural mode.
Some times I want to query just one databse, some times two of them, whatever.
What would it be the best way to connect with two of them and then, say, join two tables belonging to different databases?
Thanks in advanced!
If the databases all reside in the same server, you can query any table in any database at any time that you have access to by prefixing it with the database name:
SELECT *
FROM database1.table1
JOIN database2.table2 ON ...
If the databases are on two different servers or need different access rights, there's no way to do this.