问题如下:
表1数据
ID 日期时间 字段1
1234 2009-08-27 32.2
1235 2009-08-27 33.4
1236 2009-08-27 42.2表2数据
ID 日期时间 字段1 字段2
3234 2009-08-27 32.4 aaa
3235 2009-08-27 53.6 bbb
4236 2009-08-27 12.5 ddd
根据时间查询 2009-08-27 显示结果如下:
ID 日期时间 字段1 ID 日期时间 字段1 字段2
1234 2009-08-27 32.2 3234 2009-08-27 32.4 aaa
1235 2009-08-27 33.4 3235 2009-08-27 53.6 bbb
1236 2009-08-27 42.2 4236 2009-08-27 12.5 ddd
请问大家 SQL 怎么写呀 谢谢了
表1数据
ID 日期时间 字段1
1234 2009-08-27 32.2
1235 2009-08-27 33.4
1236 2009-08-27 42.2表2数据
ID 日期时间 字段1 字段2
3234 2009-08-27 32.4 aaa
3235 2009-08-27 53.6 bbb
4236 2009-08-27 12.5 ddd
根据时间查询 2009-08-27 显示结果如下:
ID 日期时间 字段1 ID 日期时间 字段1 字段2
1234 2009-08-27 32.2 3234 2009-08-27 32.4 aaa
1235 2009-08-27 33.4 3235 2009-08-27 53.6 bbb
1236 2009-08-27 42.2 4236 2009-08-27 12.5 ddd
请问大家 SQL 怎么写呀 谢谢了
解决方案 »
- datagridview数据绑定过程中,如何能让用户看到数据?
- c# 中怎样将datagridview的数据导出成excel文件
- 使用的是 .NET Framework 2.0,需要用 4.0 中的库,又不想升级,怎么办?
- 如何让dataGridView单元格显示按键值而不做相应动作
- 高手请进,c#三层中客户端与服务端的连接问题
- 初级:datagrid 绑定问题:this.sqlDataAdapter1.Fill(this.dataSet11); this.DataGrid1.DataBind();第一句有什么问题呀
- 求解决asp.net datagrid第一页,最后一页问题
- 只是想在类里面使用窗体控件 希望能给点代码 好懂写
- 请问MySQL是否支持COM+,可以作为COM+的RM运行
- C# 调用.dll的问题
- Lucene 索引事务要如何实现
- 急!!!C# winForm 不同窗口之间的事件调用问题
两边数据条数是否一样多..
如果一样..Union一下..
select * from 表1 where 日期时间='2009-08-27'
union all
select * from 表2 where 日期时间='2009-08-27'
肯定是不对的。
两张表都没有主外键的,怎么搞。。
如果两个表建的时候就没有关联..
在相同情况下插入的两个表..
比如一个表单上要插入两个表..但两个表建的时候没有关联..执行一个表单..表1和表2都新增一条数据..我说的是在两个表记录条数相同的前提下.. union all没有问题..
--思路:将表1中的日期为'2009-08-27'的查询出来,并加上一列序号列.同样将表2中的日期为'2009-08-27'的查询出来,并加上一列序号列.再用一个外连接将两个查询连接起来,即为所求.----表1中的日期为'2009-08-27'的查询--
SELECT 序号= (SELECT COUNT(ID) FROM 表1 AS a WHERE a.ID <= b.ID and a.日期时间 = '2009-08-27'),b.ID,b.日期时间,b.字段1 FROM 表1 AS b where b.日期时间 = '2009-08-27'--表2中的日期为'2009-08-27'的查询--
SELECT 序号= (SELECT COUNT(ID) FROM 表2 AS c WHERE c.ID <= d.ID and c.日期时间 = '2009-08-27'),d.ID,d.日期时间,d.字段1,d.字段2 FROM 表2 AS d where d.日期时间 = '2009-08-27'--最后所求--
select * from (SELECT 序号= (SELECT COUNT(ID) FROM 表1 AS a WHERE a.ID <= b.ID and a.日期时间 = '2009-08-27'),b.ID,b.日期时间,b.字段1 FROM 表1 AS b where b.日期时间 = '2009-08-27') as tab1 left join (SELECT 序号= (SELECT COUNT(ID) FROM 表2 AS c WHERE c.ID <= d.ID and c.日期时间 = '2009-08-27'),d.ID,d.日期时间,d.字段1,d.字段2 FROM 表2 AS d where d.日期时间 = '2009-08-27') as tab2 on tab1.序号 = tab2.序号
select tab1.ID,tab1.日期时间.....可以让序号列不显示出来.
create table test10 ([ID] int ,[日期时间] varchar(10) ,[字段1] varchar(10) )
insert into test10
select 1234,'2009-08-27', '32.2'
union all
select 1235,'2009-08-27', '33.4'
union all
select 1236,'2009-08-27','42.2'create table test11([ID] int,[日期时间] varchar(10) ,[字段1] varchar(10) ,[字段2] varchar(10) )
insert into test11
select 3234,'2009-08-27','32.4','aaa'
union all
select 3235,'2009-08-27','53.6','bbb'
union all
select 4236,'2009-08-27','12.5','ddd'
select (select count(*) from test10 a where a.ID <= b.ID ) as iIndex,*
into #table1
from test10 bselect (select count(*) from test11 a where a.ID <= b.ID ) as iIndex,*
into #table2
from test11 bselect a.ID,a.[日期时间],a.[字段1],b.ID,b.[日期时间],b.[字段1],b.[字段2] from #table1 a
full join #table2 b
on a.iIndex=b.iIndexdrop table test10
drop table test11
drop table #table1
drop table #table2
/*
ID 日期时间 字段1 ID 日期时间 字段1 字段2
----------- ---------- ---------- ----------- ---------- ---------- ----------
1234 2009-08-27 32.2 3234 2009-08-27 32.4 aaa
1235 2009-08-27 33.4 3235 2009-08-27 53.6 bbb
1236 2009-08-27 42.2 4236 2009-08-27 12.5 ddd*/
select a.ID,a.[日期时间],a.[字段1],b.ID,b.[日期时间],b.[字段1],b.[字段2] from #table1 a,#table2 b where a.iIndex=b.iIndex
结果一致
from 表1 A,表2 B
WHEER A.日期时间=B.日期时间 AND B.日期时间='2009-8-27'
drop table #1
create table #1([ID] int ,[日期时间] varchar(10) ,[字段1] varchar(10))
insert into #1
select 1234,'2009-08-27', '32.2'
union all
select 1235,'2009-08-27', '33.4'
union all
select 1236,'2009-08-27','42.2'if object_id('tempdb..#2') is not null
drop table #2
create table #2([ID] int,[日期时间] varchar(10) ,[字段1] varchar(10) ,[字段2] varchar(10) )
insert into #2
select 3234,'2009-08-27','32.4','aaa'
union all
select 3235,'2009-08-27','53.6','bbb'
union all
select 4236,'2009-08-27','12.5','ddd'
alter table #1 add id1 int identity(1,1)
alter table #2 add id1 int identity(1,1)
go
--select * from #1 a, #2 b where a.id1 = b.id1
select * from #1 inner join #2 on #1.id1 = #2.id1
ID 日期时间 字段1 id1 ID 日期时间 字段1 字段2 id1
----------- ---------- ---------- ----------- ----------- ---------- ---------- ---------- -----------
1234 2009-08-27 32.2 1 3234 2009-08-27 32.4 aaa 1
1235 2009-08-27 33.4 2 3235 2009-08-27 53.6 bbb 2
1236 2009-08-27 42.2 3 4236 2009-08-27 12.5 ddd 3(所影响的行数为 3 行)