想对数据库PCode的字段PCID进行整理!PCID:
P11234
P11212
P11324
P12334
P12445
P12556
P23445
P23555想对字段PCID
前3个字符为P11所有值加入到临时表A的字段a1里
前3个字符为P12所有值加入到临时表A的字段a2里
前3个字符为P23所有值加入到临时表A的字段a3里请问要怎么做会比较好些?谢谢!

解决方案 »

  1.   

    where subtract(pcid,1,3)='pl1'...........
      

  2.   

    where substring(pcid,1,3)='pl1'...........
      

  3.   

    select id,PCID into temp1 PCID from 原表
    where left(PCID,3)='P11'
    select id,PCID into temp2 PCID from 原表
    where left(PCID,3)='P12'
    select id,PCID into temp3 PCID from 原表
    where left(PCID,3)='P23'select a.PCID as a1,b.PCID as a2,c.PCID as a3 from temp1 a left join temp2 b
    on a.id=b.id
    left join temp3 c
    on a.id=c.id
    go
    drop table temp1
    drop table temp2
    drop table temp3
      

  4.   

    select PCID into temp1 from 原表 where Left(PCID,3)='P11'
      

  5.   

    left或者substring都可以判断,然后插入对应的表就ok了,上面已经写了
      

  6.   

    很不好意思!
    我现在才来看这个贴!因为我国庆有5天假,所以过了这么久才来看!我试了 chinaandys(降龙十八炒&&蛋炒饭) 兄弟的方法,但是不能执行!我自己的方法有一些缺陷,就是当Left(PCID,3)='P11'的值为空时,其他不为空值,
    但是查询临时表的值也是空值!
    我的执行方法如下:
    Create Table PCodes(sblue char(5),spurple char(5))INSERT INTO PCodes SELECT * FROM (select Distinct P_CodeID from PCodeColores where left(P_CodeID,3)='P11')as a,(select Distinct P_CodeID from PCodeColores where left(P_CodeID,3)='P12')as bselect * from PCodes看有没有更好的方法解决这个问题!