[英]Wildcards in sql
How does wildcards works in sql. 通配符如何在sql中工作。 If I do select * from table
it give all the fields. 如果我select * from table
它会给出所有字段。 But if I do select a* from table
it gives error. 但如果我select a* from table
它会给出错误。 Shouldn't it give all fields which begins with a? 它不应该给所有以a开头的字段吗? I am little confused. 我有点困惑。
SELECT * FROM tableName
literally means "select all columns from tableName". SELECT * FROM tableName
字面意思是“从tableName中选择所有列”。
Philip Graham is right about his answer where he asked to use a.*
菲利普·格雷厄姆对他的回答是正确的,他要求使用a.*
Wildcards help you search for strings about which you are not sure. 通配符可帮助您搜索您不确定的字符串。 These are almost always used with the LIKE
keyword and put in WHERE
clauses or searched CASE
statements. 它们几乎总是与LIKE
关键字一起使用,并放在WHERE
子句或搜索的CASE
语句中。
There are two wildcard characters - %
and _
. 有两个通配符 - %
和_
。
%
is used to find any string of 0 or more length. %
用于查找长度为0或更多的任何字符串。 Eg, 例如,
SELECT firstName
FROM persons
WHERE UPPER(firstName) LIKE 'J%'
This will return all the firstName
from the persons
table where firstname starts with letter J
. 这将返回所有firstName
从persons
,其中姓与字母开头表J
。 This would return "Jason", "James", "Josh", "Jessica" and much more. 这将归还“Jason”,“James”,“Josh”,“Jessica”等等。
Note that UPPER
function was used to eliminate case sensitivity. 请注意, UPPER
功能用于消除区分大小写。
Next, you can have an _
character that looks for the presence of one single character. 接下来,您可以使用_
字符查找是否存在单个字符。
SELECT firstName
FROM persons
WHERE UPPER(firstName) LIKE 'J_M__'
This would return "James", "Jimmy", "Jamos", "Jxmx" and filter away any "Jason", "Jaguar", etc. 这将返回“James”,“Jimmy”,“Jamos”,“Jxmx”并过滤掉任何“Jason”,“Jaguar”等。
You can use a.* where a is the name of the table. 您可以使用。*,其中a是表的名称。 For instance in 例如在
select a.* from a left join b on a.id = b.id
You would return only the fields from a but not from b 您只返回a中的字段而不是b中的字段
If want to use a wild card in SQL, You need to key on the column that you want to filter using LIKE
. 如果要在SQL中使用通配符,则需要使用LIKE
键入要过滤的列。
SELECT *
FROM table
WHERE column_name LIKE 'a%';
This will give you everything that begins with 'a' on that column. 这将为您提供该列上以“a”开头的所有内容。
If you don't want all the columns, you must explicitly give the name of each column that you want in the query. 如果您不想要所有列,则必须在查询中明确指定所需的每个列的名称。
SELECT LastName, FirstName, Address
FROM table
So if you want all the fields that begin with 'a' you must name all the fields that begin with 'a' in the SELECT statement. 因此,如果您想要所有以'a'开头的字段,则必须在SELECT语句中为所有以'a'开头的字段命名。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.