How to inner join two database?


Can anyone have the solution how to inner join the two different database.

You cannot inner join two databases.
You can inner join two tables.

How? Using inner join :slight_smile:

If you want help with a specific query, you’ll have to post more details.

thanks for the reply…

I have the first database which i must view all the user and the second database it is where i must save all the user.
and after saving the value on the first database it must not viewed on.

So you have two separate databases, and each database has a user table?
The two user tables are identical?

What database is it? MySQL?

slightly different the db1 contains more fields than the db2.but the content is the same.

why is the data not all in one table perhaps with a new column, to flag specific records? ie those which are currently in table two could be flagged as approved or whatever your reasoning is to identifyi them separately.


the first database is the existing one…i must get all the existing data and pass it to the second database…
the reason that i want to use the inner join is to exclude the one that i save on second database…

Or if not possible is there a way to connect two database on the same host in one php page.

I am still trying to understand what you are trying to do. is this a move to a new db or are you checking data in the first table and when checked, you want to move it to the second one?


Yup…that’s the idea…and if they have the same UserName it must be viewed.

here is my complete script…


$id = $_GET['ID'];
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<html xmlns="">
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<style type="text/css" title="currentStyle">
		@import "css/demo_page.css";
		@import "css/demo_table.css";
	<script type="text/javascript" language="javascript" src="jquery./jquery.js"></script>
	<script type="text/javascript" language="javascript" src="jquery./jquery.dataTables.js"></script>
	<script type="text/javascript" charset="utf-8">
		$(document).ready(function() {
		} );
<body id="dt_example">

<table  BORDERCOLOR="#0B0B0B" border='1' bgcolor="#F5F5FA" width="80%" align='center'>
	<td width="100%" align="left" valign="middle">
		<img src='img/viewusers.png'>




	<td width="100%" align="center" >	
		//second database,add the data from the first database
		if ($id != ""){
			mssql_query("sp_insertuser @uname='$_GET[user]',@lname='$_GET[lname]',@fname='$_GET[fname]',@email='$_GET[email]',@dept='$_GET[dept]',@Cby=$_SESSION[username],@userlvl=$_GET[lvl]");
		$uname=mssql_fetch_assoc(mssql_query("select UserName from tblUser"));
		//my first database,check the existing data
				$results=mssql_query("Select * from users where user_login!='$uname[UserName]'");
		<div id="containeradd">
			<div align='left' id="demo">
				<table BORDERCOLOR="#0B0B0B" cellpadding="0" cellspacing="0" frame="box" class="display" id="example">
							<th><a>Full Name</a></th>
						<?php while ($row=mssql_fetch_assoc($results)) { ?>
							<tr class="GradeA">
								<td class="center"><?php echo $row['user_login'];?></td>
								<td class="center"><?php echo $row['user_first_name']." ".$row['user_last_name'];?></td>
								<td class="center"><?php echo $row['user_email'];?></td>
								<td class="center"><?php echo $row['emp_department']; ?></td>
								<td class="center"><?php echo $row['usrlevel']; ?></td>
								<td class="center"><?php echo "<a href='Users.php?ID={$row['user_id']}&user={$row['user_login']}&fname={$row['user_first_name']}&lname={$row['user_last_name']}&email={$row['user_email']}&dept={$row['emp_department']}&lvl={$row['usrlevel']}' onClick=\\"javascript: var x=window.confirm('Add User ".$row['user_login']."?');if (!x) return(false);\\" title='Add User'>Add User</a>" ?></td>
						<?php }	?>
							<th>Full Name</th>
}else{ //not logged in
    header('location: login.php');

if the username is the same it must be viewed.

Does that mean, it should show up for you to check it, if they are the same people - like a manual verification?

please show your create table statement for table 1. I need to see if the username is unique because, if it is, then how could you have accidental (or any) duplicates?

and if there would be no dupes of people then when inserting to table 2, I would use ON DUPLICATE KEY UPDATE.

would something like this not work?

insert ignore into table 2
( col1, col2)
, col2
from table 1

make sure you back up the table 2 before doing that though.

you can inner join by using the plus operator on right side of equal to operator

two comments:

  1. that would make it an outer join, not an inner join

  2. that syntax is horrible as well as deprecated – use OUTER JOIN syntax if you need to have an outer join

Try this:

SELECT a.userID, b.usersFirstName, b.usersLastName FROM databaseA.dbo.TableA a
inner join database B.dbo.TableB b ON a.userID=b.userID

I have two different MSSQL databases on two different servers that i need to join.
Table1 on one server
Table 2 on the other

see post #14 in this thread


i was just wandering what is DBO?

“dbo” stands for “data base owner” and it is the owner of the database

Why in God’s name are you using 2 databases? The performance must be horrific, not to mention syncing must be a pain in the rear.

Might I recommend PHP and MySQL for Dummies with CDROM (0785555108622): Janet Valade: Books

you might

but we would like you a whole lot more if you recommended the sitepoint books on the same subject instead

also it’s a good idea to watch your language, especially the religious references