如果你两个数据库在同一个服务器上可以用
select 库1x,库2xx from 库1table1 ,库2table2 where 库1table1xxx=库2table2xxx 这种方式写
这样你只需要写一个库1的连接字符串。。。然后把语句丢给库1处理。。。
如果是两个不同服务器。。。还是用链接或存储过程吧。。。
select 字段列表 from 表1 join 表2 on 两个表之间的关联条件
select 字段列表 from 表1,表2 where 两个表之间的关联条件
上面这两句是一个意思,都属于连接查询中的内连接,返回完全满足关联条件的记录;第一句其实完整写法应该这样:select 字段列表 from 表1 inner join 表2 on 两个表之间的关联条件
-----------------------------------
inner join内部连接 两表都满足的组合
full outer 全连 两表相同的组合在一起,A表有但B表没有的数据(显示null), 同样B表有,A表没有的显示为(null)
A表 left join B表 左连,以A表为基础,A表的全部数据,B表有的组合。没有的为null
A表 right join B表 右连,以B表为基础,B表的全部数据,A表的有的组合。没有的为null
查询分析器中执行:
--建表table1,table2:
create table table1(id int,name varchar(10))
create table table2(id int,score int)
insert into table1 select 1,'lee'
insert into table1 select 2,'zhang'
insert into table1 select 4,'wang'
insert into table2 select 1,90
insert into table2 select 2,100
insert into table2 select 3,70
如表
-------------------------------------------------
table1| table2|
-------------------------------------------------
|id name| |id score|
|1 lee| |1 90|
|2 zhang| |2 100|
|4 wang| |3 70|
-------------------------------------------------
以下均在查询分析器中执行
一、外连接
1概念:包括左向外联接、右向外联接或完整外部联接
2左连接:left join 或 left outer join
(1)左向外联接的结果集包括 LEFT OUTER 子句中指定的左表的所有行,而不仅仅是联接列所匹配的行。如果左表的某行在右表中没有匹配行
,则在相关联的结果集行中右表的所有选择列表列均为空值(null)。
(2)sql语句
select from table1 left join table2 on table1id=table2id
-------------结果-------------
| id | name | id | score |
------------------------------
| 1 | lee | 1 | 90 |
| 2 | zhang | 2 | 100 |
| 4 | wang |NULL| NULL |
------------------------------
注释:包含table1的所有子句,根据指定条件返回table2相应的字段,不符合的以null显示
3右连接:right join 或 right outer join
(1)右向外联接是左向外联接的反向联接。将返回右表的所有行。如果右表的某行在左表中没有匹配行,则将为左表返回空值。
(2)sql语句
select from table1 right join table2 on table1id=table2id
-------------结果-------------
|id | name | id | score |
------------------------------
| 1 | lee | 1 | 90 |
| 2 |zhang | 2 | 100 |
|NULL| NULL | 3 | 70 |
------------------------------
注释:包含table2的所有子句,根据指定条件返回table1相应的字段,不符合的以null显示
4完整外部联接:full join 或 full outer join
(1)完整外部联接返回左表和右表中的所有行。当某行在另一个表中没有匹配行时,则另一个表的选择列表列包含空值。如果表之间有匹配行
,则整个结果集行包含基表的数据值。
(2)sql语句
select from table1 full join table2 on table1id=table2id
-------------结果-------------
| id | name | id | score |
------------------------------
| 1 | lee | 1 | 90 |
| 2 | zhang| 2 | 100 |
| 4 | wang|NULL| NULL|
|NULL| NULL| 3 | 70 |
------------------------------
注释:返回左右连接的和(见上左、右连接)
二、内连接
1概念:内联接是用比较运算符比较要联接列的值的联接
2内连接:join 或 inner join
3sql语句
select from table1 join table2 on table1id=table2id
-------------结果-------------
| id | name | id | score |
------------------------------
| 1 | lee | 1 | 90 |
| 2 | zhang| 2 | 100 |
------------------------------
注释:只返回符合条件的table1和table2的列
4等价(与下列执行效果相同)
A:select a,b from table1 a,table2 b where aid=bid
B:select from table1 cross join table2 where table1id=table2id (注:cross join后加条件只能用where,不能用on)
三、交叉连接(完全)
1概念:没有 WHERE 子句的交叉联接将产生联接所涉及的表的笛卡尔积。第一个表的行数乘以第二个表的行数等于笛卡尔积结果集的大小。
(table1和table2交叉连接产生33=9条记录)
2交叉连接:cross join (不带条件where)
3sql语句
select from table1 cross join table2
-------------结果-------------
| id | name | id | score |
------------------------------
| 1 | lee | 1 | 90 |
| 2 | zhang| 1 | 90 |
| 4 | wang| 1 | 90 |
| 1 | lee| 2 | 100 |
| 2 | zhang| 2 | 100 |
| 4 | wang| 2 | 100 |
| 1 | lee | 3 | 70 |
| 2 | zhang| 3 | 70 |
| 4 | wang| 3 | 70 |
------------------------------
注释:返回33=9条记录,即笛卡尔积
4等价(与下列执行效果相同)
A:select from table1,table2
如下方法:
select top 1 a姓名 , bminnum , bmaxnum from a INNER JOIN b ON aid = bid where bminnum > 40 or bmaxnum < 40。
1、SQL的解释:结构化查询语言(英文简称:SQL)是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统;同时也是数据库脚本文件的扩展名。它不要求用户指定对数据的存放方法,也不需要用户了解具体的数据存放方式,所以具有完全不同底层结构的不同 数据库系统,,可以使用相同的结构化查询语言作为数据输入与管理的接口。
2、SQL的语句结构:其语句,也称为“数据检索 语句”,用以从表中获得数据,确定数据怎样在应用程序给出。保留字 SELECT是DQL(也是所有SQL)用得最多的动词,其他DQL常用的保留字有WHERE,ORDER BY,GROUP BY和HAVING。这些DQL保留字常与其他类型的SQL语句一起使用。
试试 select aid from table1 as a join table2 as b where ( apid = bpid or apid like '%,||bpid or apid like '%,||bpid||,%' or apid like bpid||,%') and bname like '%def%')
select from [数据库1]dbo[表1] where 字段='' union
select from [数据库2]dbo[表2] where 字段='' 表示把查询的结果合并显示,上面那个有些问题,就试下这个吧。这个是要求两个表的结构式一样的 或者是要查询的字段结构是一样的就可以
多表联查?你具体指什么?
一般查询多个表,只用进行连接查询
selectTable1Item1,Table2Item2fromTable1,Table2就可以了阿
返回的记录集是多个表的列组合在一起的
相信你不是说这个,具体点
只要循环查询字符串里面FROM子句后面的表名就可以了。
如C#中:
string table="info_20060";
string sql="select ";
for(int i=1;i<=X;i++)
{
string temp=table+iToString();
sql =sql+ temp+"";
if(i<X) sql+= ",";
}
sql+=" from ";
for(int i=1;i<=X;i++)
{
string temp=table+iToString();
sql += temp;
if (i < X) sql += ",";
}
sql=sql+" order by info_200601sn desc";
最后的sn不知道你什么意思。
是否可以解决您的问题?
1提取单条记录
//using SystemData;
//using SystemDataSqlClient;
using (SqlConnection cn = new SqlConnection(%%1))
//ConfigurationManagerConnectionStrings["db2ConnectionString"]ConnectionString
//Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
{
SqlCommand cmd = new SqlCommand("Select Count() From jobs",cn);
cnOpen();
%%2=cmdExecuteScalar(); //MessageInnerHtml
}2单值比较
using (SqlConnection cn = new SqlConnection(%%1))
//ConfigurationManagerConnectionStrings["db2ConnectionString"]ConnectionString
//Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
{
SqlCommand cmd = new SqlCommand("Select Count() From jobs",cn);
cnOpen();
%%2=cmdExecuteScalar(); //MessageInnerHtml
if(%%2==%%3)
{
%%4
}
}
3显示表格
//using SystemData;
//using SystemDataSqlClient;
DataSet ds=null;
using (SqlConnection cn = new SqlConnection(%%1))
//ConfigurationManagerConnectionStrings["db2ConnectionString"]ConnectionString
//Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
{
cnOpen();
cmd=new SqlDataAdapter("Select From Author",cn);
ds=new DataSet();
cmdFill(ds,%%2); //"作者"
MyDataGridDataSource=dsTables(%%2)DefaultView; //"作者"
MyDataGridDataBind();
}
4 *** 作表格
//using SystemData;
//using SystemDataSqlClient;
using (SqlConnection cn = new SqlConnection(%%1))
//ConfigurationManagerConnectionStrings["db2ConnectionString"]ConnectionString
//Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
{
cnOpen();
cmd=new SqlDataAdapter("Select From Author",cn);
ds=new DataSet();
cmdFill(ds,%%2); //"作者"
MyDataGridDataSource=dsTables(%%2)DefaultView; //"作者"
MyDataGridDataBind();
}
5数值范围查询
//using SystemData;
//using SystemDataSqlClient;
using (SqlConnection cn = new SqlConnection(%%1))
//ConfigurationManagerConnectionStrings["db2ConnectionString"]ConnectionString
//Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
{
}
6关闭时断开连接
//using SystemData;
//using SystemDataSqlClient;
using (SqlConnection cn = new SqlConnection(%%1))
//ConfigurationManagerConnectionStrings["db2ConnectionString"]ConnectionString
//Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
{
}
7执行命令
//using SystemData;
//using SystemDataSqlClient;
using (SqlConnection cn = new SqlConnection(%%1))
//ConfigurationManagerConnectionStrings["db2ConnectionString"]ConnectionString
//Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
{
SqlCommand cmd = new SqlCommand("insert userRegister_t values('"
+ TextBox1Text + "','" + TextBox2Text + "')",cn);
cnOpen();
cmdExecuteNonQuery();
}
以上就是关于sql数据库 多个数据库进行关联查询 求助全部的内容,包括:sql数据库 多个数据库进行关联查询 求助、数据库连接查询、SQL查询两个表联合查询怎么写等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)