Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何找到剩余的字母表?_Sql Server - Fatal编程技术网

Sql server 如何找到剩余的字母表?

Sql server 如何找到剩余的字母表?,sql-server,Sql Server,我有一个表,我想写一个查询来获取数据 empno empname 1 abc 2 xyz 3 mnc 4 pqr 现在我想把empname中没有使用的字母作为第一个字母 b、 c,d,e,f,g,h,i,j,k,l,n,o,q,r,s,t,u,v,w,y,z 我不需要empname a、 x,m,p 那么,我如何为它编写查询呢 提前感谢使用下面的查询 select * from employee where empname not like

我有一个表,我想写一个查询来获取数据

empno empname 
  1     abc
  2     xyz
  3     mnc
  4     pqr
现在我想把
empname
中没有使用的字母作为第一个字母

b、 c,d,e,f,g,h,i,j,k,l,n,o,q,r,s,t,u,v,w,y,z

我不需要
empname

a、 x,m,p

那么,我如何为它编写查询呢

提前感谢

使用下面的查询

select * from employee
  where empname not like 'a%' 
    and empname not like 'x%' 
    and empname not like 'm%' 
    and empname not like 'p%'
这里的“%”用于选择名称的第一个字符

使用下面的查询

select * from employee
  where empname not like 'a%' 
    and empname not like 'x%' 
    and empname not like 'm%' 
    and empname not like 'p%'
这里的“%”用于选择名称的第一个字符


首先,只需使用一个表变量来存储所有字母,如下所示

declare @alphabets as table(letter varchar(1));

insert into @alphabets values
('a'),
('b'),
('c'),
('d'),
('e'),
('f'),
('g'),
('h'),
('i'),
('j'),
('k'),
('l'),
('m'),
('n'),
('o'),
('p'),
('q'),
('r'),
('s'),
('t'),
('u'),
('v'),
('w'),
('x'),
('y'),
('z');
然后使用
notexists
查找表变量中第一个字符为
empname
的缺失字母表,并使用
STUFF
将所有字符用逗号连接起来

查询

select stuff((select ', ' + t.letter from(
    select * from @alphabets a
    where not exists(
        select 1 from [your_table_name] e
        where a.letter = left(e.empname, 1)
    )
)t
for xml path('')
), 1, 2, '');

首先使用一个表变量来存储所有字母,如下所示

declare @alphabets as table(letter varchar(1));

insert into @alphabets values
('a'),
('b'),
('c'),
('d'),
('e'),
('f'),
('g'),
('h'),
('i'),
('j'),
('k'),
('l'),
('m'),
('n'),
('o'),
('p'),
('q'),
('r'),
('s'),
('t'),
('u'),
('v'),
('w'),
('x'),
('y'),
('z');
然后使用
notexists
查找表变量中第一个字符为
empname
的缺失字母表,并使用
STUFF
将所有字符用逗号连接起来

查询

select stuff((select ', ' + t.letter from(
    select * from @alphabets a
    where not exists(
        select 1 from [your_table_name] e
        where a.letter = left(e.empname, 1)
    )
)t
for xml path('')
), 1, 2, '');

您是如何知道
empname
的第一个字母中只有这4个字符的?您是如何知道
empname
的第一个字母中只有这4个字符的?我检查了您的查询,但它不起作用。您能解释一下吗?为什么它不起作用?将查询与表变量
@alphabets
一起运行。检查我答案中的演示链接。同时将
[您的表名]
更改为您的实际表名。我检查了您的查询,但它没有工作。您能解释一下吗?为什么它没有工作?将查询与表变量
@alphabets
一起运行。检查我答案中的演示链接。还可以使用实际的表名更改
[您的表名]