Thursday, May 31, 2012

mysql advance query


copy one row to insert into the same table?

INSERT INTO table (col1, col2, col3, ...)
SELECT col1, col2, col3, ... FROM table


Fetch from one table and insert into another table single query

INSERT INTO table (col1, col2, col3, ...)
SELECT col1, col2, col3, ... FROM table2


Fetch from one table a database and insert into another table another database single query single query

INSERT INTO database1.table (col1, col2, col3, ...)
SELECT col1, col2, col3, ... FROM database2.table



How do you connect to multiple MySQL databases on a single webpage?


You can make multiple calls to mysql_connect(), but if the parameters are the same you need to pass true for the '$new_link' (fourth) parameter, otherwise the same connection is reused.
so then you have
$dbh1 = mysql_connect($hostname, $username, $password); 

$dbh2 = mysql_connect($hostname, $username, $password, true); 



mysql_select_db('database1', $dbh1);

mysql_select_db('database2', $dbh2);



Then to query database 1, do
mysql_query('select * from tablename', $dbh1);



and for database 2
mysql_query('select * from tablename', $dbh2);

select with select in where clause

select * from value where id in (select id from value where id=1000000); 

No comments:

Post a Comment