SQL 如何在sql中显示以a开头然后b开头的员工姓名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3739365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:35:40  来源:igfitidea点击:

how to display employee names starting with a and then b in sql

sql

提问by Linux world

i want to display the employee names which having names start with a and b ,it should be like list will display employees with 'a' as a first letter and then the 'b' as a first letter...

我想显示名称以 a 和 b 开头的员工姓名,应该像列表将显示员工的第一个字母为 'a',然后是 'b' 作为第一个字母...

so any body tell me what is the command to display these...

所以任何人都告诉我显示这些的命令是什么......

回答by Martin Smith

To get employee names starting with A or B listed in order...

要按顺序列出以 A 或 B 开头的员工姓名...

select employee_name 
from employees
where employee_name LIKE 'A%' OR employee_name LIKE 'B%'
order by employee_name

If you are using Microsoft SQL Server you could use

如果您使用的是 Microsoft SQL Server,则可以使用

....
where employee_name  LIKE '[A-B]%'
order by employee_name

This is not standard SQL though it just gets translated to the following which is.

这不是标准 SQL,尽管它只是被翻译成以下内容。

WHERE  employee_name >= 'A'
       AND employee_name < 'C' 

For all variants you would need to consider whether you want to include accented variants such as áand test whether the queries above do what you want with these on your RDBMS and collation options.

对于所有变体,您需要考虑是否要包含重音变体,例如á并测试上面的查询是否在 RDBMS 和排序规则选项上执行您想要的操作。

回答by cfEngineers

select columns
  from table
 where (
         column like 'a%' 
      or column like 'b%' )
 order by column asc

回答by Crazy Cat

Regular expressions work well if needing to find a range of starting characters. The following finds all employee names starting with A, B, C or D and adds the “UPPER” call in case a name is in the database with a starting lowercase letter. My query works in Oracle (I did not test other DB's). The following would return for example:
Adams
adams
Dean
dean

如果需要查找一系列起始字符,正则表达式可以很好地工作。下面查找所有以 A、B、C 或 D 开头的员工姓名,并添加“UPPER”调用,以防数据库中的姓名以小写字母开头。我的查询在 Oracle 中有效(我没有测试其他数据库)。下面将返回例如:
亚当

院长
院长

This query also ignores case in the ORDER BY via the "lower" call:

此查询还通过“lower”调用忽略 ORDER BY 中的大小写:

SELECT employee_name 
FROM employees
WHERE REGEXP_LIKE(UPPER(TRIM(employee_name)), '^[A-D]')
ORDER BY lower(employee_name)

回答by Russel Allado

select employee_name 
from employees
where employee_name LIKE 'A%' OR employee_name LIKE 'B%'
order by employee_name

回答by prashant

Here what I understood from the question is starting with "a " and then "b" ex:

在这里,我从问题中了解到的是以“a”开头,然后是“b”,例如:

  • abhay
  • abhishek
  • abhinav
  • 阿拜
  • 阿布舍克
  • 阿比纳夫

So there should be two conditions and both should be true means you cant use "OR" operator Ordered by is not not compulsory but its good if you use.

所以应该有两个条件,并且都应该是真的,这意味着你不能使用“OR”运算符 Ordered by 不是强制性的,但如果你使用它就很好。

Select e_name from emp 
where  e_name like 'a%' AND e_name like '_b%'
Ordered by e_name

回答by Jay

What cfengineers said, except it sounds like you will want to sort it as well.

cfengineers 所说的,但听起来您也想对其进行排序。

select columns
  from table
 where (
         column like 'a%' 
      or column like 'b%' )
order by column

Perhaps it would be a good idea for you to check out some tutorials on SQL, it's pretty interesting.

也许查看一些有关 SQL 的教程对您来说是个好主意,它非常有趣。

回答by Purge

If you're asking about alphabetical order the syntax is:

如果您询问字母顺序,则语法为:

SELECT * FROM table ORDER BY column

the best example I can give without knowing your table and field names:

我可以在不知道您的表和字段名称的情况下给出的最佳示例:

SELECT * FROM employees ORDER BY name

回答by Linux world

select * 
from stores 
where name like 'a%' or 
name like 'b%' 
order by name

回答by JohnB

select name_last, name_first
from employees
where name_last like 'A%' or name_last like 'B%'
order by name_last, name_first asc

回答by Mohit Tejwani

From A to Z:

从A到Z:

select employee_name from employees ORDER BY employee_name ;

From Z to A:

从 Z 到 A:

select employee_name from employees ORDER BY employee_name desc ;