alist主表.记录每样物品的详细信息.
bopen次表.记录用户每次打开物品的记录.
每次A用户或B用户打开BOPEN后,BOPEN就会生产一条记录.
SQL生成了一个
case when not XXXX is null then '1' else '0' end ynopen 
如果有记录ynopen的值就为1,如果没有记录ynopen就为0现用
select alist.id as aid,
case when not b.autoid is null then '1' else '0' end YNopen
from alist left join bopen on alist.id=b.id
where 条件语句
如此一来,会出现大量结果
于是就用了distinct进行精简重复的select alist.id as aid,
case when not b.id is null then '1' else '0' end YNopen
from alist left join bopen on alist.id=b.id and username='a'
where 条件语句(主alist有2万多条记录.bopen打开记录也有1万条(以后会增加到上百万))
用distinct精简后,可以实现ynopen的效果,但速度非常慢,平均会用7秒的时间.
其实对比的时候,我只需要判断bopen是否有用户的记录即可,如何写?
如果有记录,ynopen就为1,没有就为0,以标示用户是否打开过.

解决方案 »

  1.   

    tryselect alist.id as aid,
    case when not Max(b.autoid) is null then '1' else '0' end YNopen
    from alist left join bopen b on alist.id=b.id
    Group By alist.id
      

  2.   

    或者select alist.id as aid,
    case when not b.autoid is null then '1' else '0' end YNopen
    from alist left join (Select id, Max(autoid) As autoid From bopen Group By id) b on alist.id=b.id
      

  3.   

    如果只仅仅是"我只需要判断bopen是否有用户的记录即可,如何写?"只要知道哪些用户有打开过就行了.select min(id) from bopen group by id
      

  4.   

    sgucxc0(ben) ( ) 信誉:100    Blog   加为好友  2007-07-13 16:46:56  得分: 0  
     
     
       如果只仅仅是"我只需要判断bopen是否有用户的记录即可,如何写?"只要知道哪些用户有打开过就行了.select min(id) from bopen group by id
      
     
    -----------
    題意沒看完整
      

  5.   

    paoluo(一天到晚游泳的鱼) 的select alist.id as aid,
    case when not b.autoid is null then '1' else '0' end YNopen
    from alist left join (Select id, Max(autoid) As autoid From bopen Group By id) b on alist.id=b.id()内变成
    Select id, Max(autoid) As autoid From bopen where username='用户名' Group By id
    的时候,OK。
    再添加ID对应时,出错?select alist.id as aid,
    case when not b.autoid is null then '1' else '0' end YNopen
    from alist left join (Select id, Max(autoid) As autoid From bopen where username='用户名' and alist.autoid=b.id Group By id,aid) b on alist.id=b.id提示
    列前缀 'alist' 与查询中所用的表名或别名不匹配。
      

  6.   

    and alist.autoid=b.id 加多这句出错.
    正确写法应该是
    and 主表alist.ID=打开表bopen.id
    作用就是
    在列出主表所有记录后.会有一个主产品的ID号,检测BOPEN每次打开的记录表是否A用户打开过这个主产品ID
      

  7.   

    全部产品表结构alist
    id(编号) 名称(name)
    1        aa
    2        a2
    3        a3
    4        a4
    5        b5
    6        7b
    .....用户打开记录表
    autoid(自动编号)  username(用户名)  id(产品id)  otime(打开时间)
    1                 a                 2           2007-5-1
    2                 a                 2           2007-5-1
    3                 b                 1           2007-5-1
    4                 a                 2           2007-5-3
    5                 b                 3           2007-5-3
    6                 a                 1           2007-5-4
    7                 a                 6           2007-5-4
    8                 a                 6           2007-5-4先列出所有产品的记录,并且列出a用户是否有打开过该产品,用户打开记录表有出现a用户打开3次产品2的记录,时间分别是5.1,5.1,5.3,如果存在记录,1条就可以判断出来了.要求用sql快速列出如下结果
    id(编号) 名称(name)  YNopen 
    1        aa           1
    2        a2           1
    3        a3           0
    4        a4           0
    5        b5           0
    6        7b           1
    如果用户打开记录表每产品只有一条记录的话,速度是非常快的,问题是用户打开记录表会记录多次打开的记录,所以要想办法在进行left join的时候就进行top 1 是否打开的操作.
    我试了paoluo(一天到晚游泳的鱼) 的,发现在()内的条件,是不能再调用()外的东西的...
      

  8.   

    gddd(gddd) ( ) 信誉:100    Blog   加为好友  2007-7-14 0:25:28  得分: 0  
     
     
       
    paoluo(一天到晚游泳的鱼) 的select alist.id as aid,
    case when not b.autoid is null then '1' else '0' end YNopen
    from alist left join (Select id, Max(autoid) As autoid From bopen Group By id) b on alist.id=b.id()内变成
    Select id, Max(autoid) As autoid From bopen where username='用户名' Group By id
    的时候,OK。
    再添加ID对应时,出错?select alist.id as aid,
    case when not b.autoid is null then '1' else '0' end YNopen
    from alist left join (Select id, Max(autoid) As autoid From bopen where username='用户名' and alist.autoid=b.id Group By id,aid) b on alist.id=b.id提示
    列前缀 'alist' 与查询中所用的表名或别名不匹配。
      
     
    -------------你直接用這個就可以了,不用做修改的,你改了反而是錯的。select alist.id as aid,
    case when not b.autoid is null then '1' else '0' end YNopen
    from alist left join (Select id, Max(autoid) As autoid From bopen Group By id) b on alist.id=b.id
      

  9.   

    --創建測試環境
    Create Table alist
    (id Int,
     name Varchar(10))
    Insert alist Select 1,        'aa'
    Union All Select 2,        'a2'
    Union All Select 3,        'a3'
    Union All Select 4,        'a4'
    Union All Select 5,        'b5'
    Union All Select 6,        '7b'Create Table bopen 
    (autoid Int,
     username Varchar(10),
     id Int,
     otime DateTime)
    Insert bopen Select 1,                 'a',                 2,           '2007-5-1'
    Union All Select 2,                 'a',                 2,           '2007-5-1'
    Union All Select 3,                 'b',                 1,           '2007-5-1'
    Union All Select 4,                 'a',                 2,           '2007-5-3'
    Union All Select 5,                 'b',                 3,           '2007-5-3'
    Union All Select 6,                 'a',                 1,           '2007-5-4'
    Union All Select 7,                 'a',                 6,           '2007-5-4'
    Union All Select 8,                 'a',                 6,           '2007-5-4'
    GO
    --測試
    select alist.id as aid,
    case when not b.autoid is null then '1' else '0' end YNopen
    from alist left join (Select id, Max(autoid) As autoid From bopen Group By id) b on alist.id=b.id
    GO
    --刪除測試環境
    Drop Table alist, bopen
    --結果
    /*
    aid YNopen
    */
      

  10.   

    --結果
    /*
    aid YNopen
    1 1
    2 1
    3 1
    4 0
    5 0
    6 1
    */
      

  11.   

    gddd(gddd) ( ) 信誉:100    Blog   加为好友  2007-7-14 0:29:01  得分: 0  
     
     
       
    and alist.autoid=b.id 加多这句出错.
    正确写法应该是
    and 主表alist.ID=打开表bopen.id
    作用就是
    在列出主表所有记录后.会有一个主产品的ID号,检测BOPEN每次打开的记录表是否A用户打开过这个主产品ID
    -----------------------首先,“and alist.autoid=b.id”,加這個肯定是錯的,倆表不受歡迎這麼對應的。其次,“正确写法应该是 and 主表alist.ID=打开表bopen.id”,這個沒有必要在內部子查詢中加上, 外面的關聯條件已經有了“on alist.id=b.id”。語句已經是OK的,為什麼還要做更改?
      

  12.   


    要求用sql快速列出如下结果
    id(编号) 名称(name)  YNopen 
    1        aa           1
    2        a2           1
    3        a3           0
    4        a4           0
    5        b5           0
    6        7b           1
    的时候。。有一点没写明白.要求用sql快速列出如下结果
    =>
    要求用sql快速列出username:a用户名为a的是否打开过的结果
    目前已解决.用
    left join (Select id,username From bopen t where not exists(select 1 from bopen  where username='a' and t.id=id and id<t.id)) D on alist.id=bopen.id
    LEFT JOIN的时候,指定WHERE NOT EXITS打条件就可以调用上一层的临时T的记录了.此效率是最快的.遇到有符合条件的就退出.另一种,效率很低:
    left join (Select id,username From bopen t where not exists(select 1 from bopen  where t.id=id and id<t.id)) D on alist.id=bopen.id and bopen.username='a'
    估计是表中存在多次记录的问题.如果确定表中只有1次记录出现的话,可以用第二种方法.