This question already has answers here:
PHP Mysql joins across databases
(4 answers)
Closed 9 years ago.
Is it possible to join 2 different databases in mysql using php?
for instance:
$sql = "SELECT * FROM db1.table LEFT JOIN db2.table USING (id)";
$result = mysql_query($sql);
--
I know how to create multiple new different database connections using php. But I'm unable to figure out if it's possible to acutally join two different databases in mysql using php in one query.
I believe you can do it simply like:
SELECT DB1Table.columnIWant, DB2Table.ColumnIWant
FROM DB1.AppropiateTable as DB1Table
JOIN DB2.TableToJoin as DB2Table
ON DB1Table.ID = DB2Table.ID
Assuming the user is auth'ed for both tables. Also - see this link on multiple connections:
How do you connect to multiple MySQL databases on a single webpage?
Related
This question already has answers here:
php and mysql copy record from one table to another
(4 answers)
Closed 3 years ago.
Is there any easy way to copy one a column in a table to another table without changing the order ?
I am a beginner here. I would be much thankful to you if you could supply in depth answer.
I think this query can help you out
INSERT INTO second_table SELECT * FROM first_table WHERE id IN(1,2,3,4...n)
This question already has answers here:
Mysqli join tables from 2 different databases
(3 answers)
Closed 4 years ago.
I am trying to connect to 2 databases on the same query of MySQL. but query doesn't work... I do not know where I'm doing wrong.. Any ideas?
$link1 = mysqli_connect("localhost","username1","pass1","db1");
$link2 = mysqli_connect("localhost","username2","pass2","db2");
#$sql = mysqli_query("SELECT db1.* FROM db1.videos WHERE db1.VID NOT IN (SELECT VID FROM db2.videos)");
You can only use one connection for a particular query, and it has to be provided as the first argument to mysqli_query(). If you want to access both databases in the same query, you need to use a connection with a username that has access to both databases. You don't need to use a different connection just because you're accessing different databases -- the database in the connection is just the initial default, but you can change it with mysqli_select_db(), and override it temporarily in a query by using the database prefix on the table name.
You can't refer to db1.* in a query, you have to specify the table name db1.videos.*
So if username1 has permission to read both db1 and db2, you can do:
$sql = mysqli_query($link1, "SELECT db1.videos.* FROM db1.videos WHERE db1.VID NOT IN (SELECT VID FROM db2.videos)");
This question already has answers here:
"Show databases" with condition
(4 answers)
Closed 5 years ago.
Is there a way to show a list of databases by condition?
Show databases WHERE table_name ='?' AND column_name = '?';
BEST BET:
Just itterate through all the databases, but if your trying to find a specific table why not just do this SHOW TABLES LIKE 'my_table_numer_%'; That will select a table with a begging part followed by a wild card text.
This question already has answers here:
Join between tables in two different databases?
(4 answers)
Closed 6 years ago.
I have a project where I have two MySQL databases. They are completly separated. They are on two different servers. There is no way to use just one.
Is it possible to connect them both together that I could use e.g. LEFTJOIN?
Or do I have to split it in two separate queries and compare them by myself?
As long as a db user used to connect to first database has the access to the second, you can do cross-db joins, you just need to specify them:
SELECT db1.table.field, db2.othertable.otherfield
FROM db1.table
JOIN db2.othertable ...
This question already has answers here:
PDO support for multiple queries (PDO_MYSQL, PDO_MYSQLND)
(7 answers)
Closed 8 years ago.
My select statement looks somewhat like this :
CREATE TEMPORARY TABLE t1 AS(
SELECT id, COUNT(*) AS count
FROM some_other_table
GROUP BY id);
ALTER TABLE t1 ADD UNIQUE INDEX (id);
SELECT * FROM t2 INNER JOIN t1 ON t1.id = t2.id
I'm using the following PHP code :
$pdo->query($sql)->fetchAll();
But I get an error since PDO does not allow multiple statements' execution in one query.
From what I've read so far, I should use exec(). But then exec() does not return results for select statement. I do not need the parametrization for this specific query so any unsafe method will work too, since the query itself is perfectly safe from any outside alteration.
Right now what I'm doing is executing the sql code as 3 different statements. but I believe that's slower than executing it in one go and would like to find a better method to do this.
Like many other inexperienced developers, you are looking wrong way trying to optimize your code.
While it doesn't really matter how many calls did you to database, the very idea of creating temporary table IS what makes your code inefficient. And you have to get rid of this really slow part instead of trying to optimize it for 0.00001%