有一个表结构如下:userID userName email
001    aaa      [email protected]
002    bbb      [email protected]
003    ccc      [email protected]
004    ddd      [email protected]
005    eee      [email protected]
就一个表,我想得到跟userID=003用户所用的email地址@后面一样的所有的用户名。
这个模糊查询该怎么写?
关键就是单引号没整明白。
备注:只用一个SQL,不能用接字符串比较的形式,只想用like模糊查询,因为我要用到这个处理,就用上面那个做例子了!。

解决方案 »

  1.   

    --就一个表,我想得到跟userID=003用户所用的email地址@后面一样的所有的用户名。
    select userid,username,email 
    from tb where userid='003' and email like '_[126.com]'
      

  2.   

    --就一个表,我想得到跟userID=003用户所用的email地址@后面一样的所有的用户名。
    select userid,username,email 
    from tb where userid='003' and email like '_126.com'
      

  3.   


    SQL> with t as(
      2       select '001' userID,'aaa' userName,'[email protected]' email from dual union all
      3       select '002','bbb','[email protected]' from dual union all
      4       select '003','ccc','[email protected]' from dual union all
      5       select '004','ddd','[email protected]' from dual union all
      6       select '005','eee','[email protected]' from dual
      7       )
      8  select userID,email
      9  from t
     10  where email like '%126%';USERID EMAIL
    ------ ------------
    003    [email protected]
    005    [email protected]
      

  4.   

    select username from tb email like '%126.com';
    --126.com 通过下面的sql获取
    select substrb(email,instrb(email,'@',1) + 1) from tb where userid='003';
      

  5.   


    with t as(
         select '001' userID,'aaa' userName,'[email protected]' email from dual union all
         select '002','bbb','[email protected]' from dual union all
         select '003','ccc','[email protected]' from dual union all
         select '004','ddd','[email protected]' from dual union all
         select '005','eee','[email protected]' from dual)
    select userID,userName,email
    from t
    where email like '%126.com';
    /*
    USERID USERNAME EMAIL
    ------ -------- ------------
    003    ccc      [email protected]
    005    eee      [email protected]
    */
      

  6.   

    我觉得你的需求提得不对哟。
    我想得到跟userID=003用户所用的email地址@后面一样的所有的用户名。
    如果 userid=003  但是@163.com 只有一个 .其余全是@126.com
    我是取@126.com一样的呢 还是 取@163.com的呢?
      

  7.   

     CREATE TABLE "SCOTT"."TEST_REGEX" 
       ( "USER_ID" VARCHAR2(20 BYTE), 
    "USER_NAME" VARCHAR2(20 BYTE), 
    "EMAIL" VARCHAR2(20 BYTE)
       ) TABLESPACE "USERS" ;
    SQL> conn scott/tiger
    已连接。
    SQL> select user_name
      2  from TEST_REGEX
      3  where email like '%'||regexp_substr(
      4  (
      5  select email
      6  from TEST_REGEX
      7  where user_id='003'
      8  )
      9  ,'@.+'
     10  );USER_NAME
    --------------------
    ccc
    eee
    select user_name
    from TEST_REGEX
    where email like '%'||regexp_substr(
    (
    select email
    from TEST_REGEX
    where user_id='003'
    )
    ,'@.+'
    )
      

  8.   

    SELECT userName
      FROM user_table a
     WHERE EXISTS (
              SELECT '*'
                FROM user_table
               WHERE SUBSTR (email, INSTR (email, '@') + 1) = SUBSTR (a.email, INSTR (a.email, '@') + 1)
                 AND userID = '003')
      

  9.   

    建议把email地址截成两段存储,@之前一个字段,@之后一个字段。
      

  10.   

    如果使用的是10G或以上的数据库,可以使用以下的语句:
    select * from table_name where regexp_like (email,'@126\.com','i');
      

  11.   


    万一userId=003的emial变成[email protected]了,岂不要修改sql?
      

  12.   

    如果业务上就有这要求那么增加一个列就是比较好的办法,这种 like 查询速度比较慢。
      

  13.   

    顶一个!这样可以对@之后的字段建立索引,以提高查询速度。通过使用类似 like '%@126.com' 这样的方式的查询不会使用索引,所以楼主需要优化一下这个查询!
      

  14.   


     
    SQL> 
    SQL> with t as(
      2       select '001' userID,'aaa' userName,'[email protected]' email from dual union all
      3       select '002','bbb','[email protected]' from dual union all
      4       select '003','ccc','[email protected]' from dual union all
      5       select '004','ddd','[email protected]' from dual union all
      6       select '005','eee','[email protected]' from dual)
      7  select t.*
      8  from t,(select substr(email,instr(email,'@',-1)+1) email from t where userid='003') tb
      9  where regexp_instr(t.email,'@'||tb.email)>0
     10  /
     
    USERID USERNAME EMAIL
    ------ -------- ------------
    003    ccc      [email protected]
    005    eee      [email protected]
     
    SQL> 
      

  15.   

    select * from usertable where userid<>'003' and email = (select top 1 substring(email,pos('@',email)+1,length(email)) from usertable where userid='003')
      

  16.   


    select username from table where substr(instr(email,'@')+1,LENGTH(email)) =
    (select substr(instr(email,'@')+1,LENGTH(email)) from table where userName = '003')
      

  17.   

    这是不用like的,用like的我再想想:select * from testt where substr(email,instr(email,'@')) =
    (select substr(email,instr(email,'@')) from testt where userid = 3);
      

  18.   

    呵呵,lz看这个~可以!!!!!!!select * from testt where email like '%'||(select substr(email,instr(email,'@')) from testt where userid = 2);
      

  19.   

    不考虑效率或者版本的差异,19楼是最直观的解法。找到某个用户的EMAIL,然后用所有用户的EMAIL去LIKE。
      

  20.   

    我跟着凑个热闹,哈哈。table1
    userID userName email
    001 aaa [email protected]
    002 bbb [email protected]
    003 ccc [email protected]
    004 ddd [email protected]
    005 eee [email protected] t2.* from table1 t1,table1 t2
    where t1.userID = '003' and
          t2.email like '%'||substr(t1.email,instr(t1.email,'@')+1)
      

  21.   

    不好意思各位!过了个节这事差点给忘了!
    题目可能没写明白,此处userId是从前台动态输入的,
    做沙发者都有点理解错误!
    不过先谢谢各位了,7楼是我最想要的结果!!
    其他也有好多答案挺好的,为啥我当时就想不出来呢~~
    先结贴了~~谢谢各位!!