有一张表是这样的,
workTable(id(主键,自动增长),work_name,work_date,work_content,status)
数据:
1,张三,2012-06-23,test, 0
2,张三,2012-06-24,test2,0
3,张三,2012-06-26,test4,0我想查询出23号到26号的数据,格式如下:
张三 2012-06-23 test    0
张三 2012-06-24 test2   0
null    null    null   null 
张三 2012-06-26 test4   025号是没数据的,我想显示出来。麻烦各位了,小弟我SQL实在很老火。

解决方案 »

  1.   

    类似如下,左连接
    select * from A left join B on A.id=B.id where xxxxx
      

  2.   

    我想查询出23号到26号的数据,格式如下:
    张三 2012-06-23 test 0
    张三 2012-06-24 test2 0
    null null null null  
    张三 2012-06-26 test4 0
      

  3.   

    直接就select * from workTable where id=25;
      

  4.   

    如果这样处理的话,我想可以如下来做
    1.将结果集填充到DataSet 中
    2.对DataSet中的结果集进行二次处理
     比如遍历DataTable 在不存在的日期上加入空行,然后返回
      

  5.   


    我想看一下是否有SQL语句能实现这样的显示。
    自己SQL比较老火,想看一下也学习一下。
      

  6.   


    select * into #workTable from (
    select 1 as id ,'张三' as work_name,'2012-06-23' as work_date,'test' as work_content, 0 as status
    union
    select 2,'张三','2012-06-24','test2',0
    union
    select 3,'张三','2012-06-26','test4',0
    union
    select 4,'张三','2012-06-28','test4',0
    union
    select 5,'张三','2012-06-30','test4',0) a
    insert into #workTable 
    select id,'',convert(varchar(10),dateadd(day,1,work_date),120),'',0
    from #workTable
    where dateadd(day,1,work_date) not in (select work_date from #workTable)
    and ID <> (select MAX(id) from #workTable)select * from #workTable order by ID,work_date
    数据源:
    1 张三 2012-06-23 test 0
    2 张三 2012-06-24 test2 0
    3 张三 2012-06-26 test4 0
    4 张三 2012-06-28 test4 0
    5 张三 2012-06-30 test4 0结果:
    1 张三 2012-06-23 test 0
    2 张三 2012-06-24 test2 0
    2 2012-06-25 0
    3 张三 2012-06-26 test4 0
    3 2012-06-27 0
    4 张三 2012-06-28 test4 0
    4 2012-06-29 0
    5 张三 2012-06-30 test4 0
      

  7.   

    改了一下select * into #workTable from (
    select 1 as id ,'张三' as work_name,'2012-06-18' as work_date,'test' as work_content, 0 as status
    union
    select 2,'张三','2012-06-22','test2',0
    union
    select 3,'张三','2012-06-26','test4',0
    union
    select 4,'张三','2012-06-28','test4',0
    union
    select 5,'张三','2012-06-30','test4',0) a
    while exists(select 1 from #workTable where dateadd(day,1,work_date) not in (select work_date from #workTable)
    and ID <> (select MAX(id) from #workTable))
    begin
    insert into #workTable 
    select id,'',convert(varchar(10),dateadd(day,1,work_date),120),'',0
    from #workTable
    where dateadd(day,1,work_date) not in (select work_date from #workTable)
    and ID <> (select MAX(id) from #workTable)
    endselect * from #workTable order by ID,work_date
      

  8.   

    如果你的work_date字段是datetime类型的:select * from workTable where work_date betwwen '2012-06-23 00:00:00' and '2012-06-26 23:59:00'
    如果是nvarchar或者别的什么的,要用游标遍历每一条数据判断是不是'2012-06-23'、'2012-06-24'、'2012-06-25'、'2012-06-26'如果输入插入到临时表中,当遍历完毕临时表中的数据就是你想要的