I'm sure this is super easy, but can't seem to figure it out.. I need to select all titles from my database where the title starts with A, or B, or C etc. Here's what I've tried so far:
我确信这非常容易,但似乎无法弄清楚..我需要从我的数据库中选择标题以A,或B或C等开头的所有标题。这是我尝试过的远:
SELECT * FROM weblinks WHERE catid = 4 AND title LIKE 'A'
but returns nothing.. Could someone help me out with this?
但什么也没有回报..有人可以帮我解决这个问题吗?
Cheers
干杯
5 个解决方案
#1
50
For titles starting in 'A' use a %
after the A
对于以'A'开头的标题,请在A之后使用%
SELECT * FROM weblinks WHERE catid = 4 AND title LIKE 'A%'
For titles with the letter 'A' in it, use %
on either side of A
对于包含字母“A”的标题,请在A的任一侧使用%
SELECT * FROM weblinks WHERE catid = 4 AND title LIKE '%A%'
For titles ending in the letter 'A', use %
before the A
对于以字母“A”结尾的标题,请在A之前使用%
SELECT * FROM weblinks WHERE catid = 4 AND title LIKE '%A'
Basically %
is a wildcard. It tells MySQL that anything can be in the location.
基本上%是一个通配符。它告诉MySQL任何东西都可以在该位置。
For having numbers as the first letter, check out Mark's answer.
有了数字作为第一个字母,请查看Mark的答案。
#2
6
The wildcards for LIKE
are %
and _
, where % matches 0 or more characters and _ matches exactly one character.
LIKE的通配符是%和_,其中%匹配0个或更多字符,_匹配一个字符。
#3
6
The existing answers are correct for beginning with A:
从A开始,现有答案是正确的:
SELECT * FROM weblinks WHERE catid = 4 AND title LIKE 'A%'
For beginning with any number you can use the REGEXP operator:
对于以任何数字开头,您可以使用REGEXP运算符:
SELECT * FROM weblinks WHERE catid = 4 AND title REGEXP '^[0-9]'
#4
2
try:
尝试:
SELECT * FROM weblinks WHERE catid = 4 AND ((title like 'A%') OR (title like 'B%'))
so on and so forth
等等等等
#5
1
SELECT * FROM weblinks WHERE catid = 4 AND title LIKE 'A%'
SELECT * FROM weblinks WHERE catid = 4 AND title LIKE'A%'
% tells "anything", so it's "A" then anything. Only works with the LIKE comparison operator.
%告诉“任何东西”,所以它是“A”然后是任何东西。仅适用于LIKE比较运算符。
#1
50
For titles starting in 'A' use a %
after the A
对于以'A'开头的标题,请在A之后使用%
SELECT * FROM weblinks WHERE catid = 4 AND title LIKE 'A%'
For titles with the letter 'A' in it, use %
on either side of A
对于包含字母“A”的标题,请在A的任一侧使用%
SELECT * FROM weblinks WHERE catid = 4 AND title LIKE '%A%'
For titles ending in the letter 'A', use %
before the A
对于以字母“A”结尾的标题,请在A之前使用%
SELECT * FROM weblinks WHERE catid = 4 AND title LIKE '%A'
Basically %
is a wildcard. It tells MySQL that anything can be in the location.
基本上%是一个通配符。它告诉MySQL任何东西都可以在该位置。
For having numbers as the first letter, check out Mark's answer.
有了数字作为第一个字母,请查看Mark的答案。
#2
6
The wildcards for LIKE
are %
and _
, where % matches 0 or more characters and _ matches exactly one character.
LIKE的通配符是%和_,其中%匹配0个或更多字符,_匹配一个字符。
#3
6
The existing answers are correct for beginning with A:
从A开始,现有答案是正确的:
SELECT * FROM weblinks WHERE catid = 4 AND title LIKE 'A%'
For beginning with any number you can use the REGEXP operator:
对于以任何数字开头,您可以使用REGEXP运算符:
SELECT * FROM weblinks WHERE catid = 4 AND title REGEXP '^[0-9]'
#4
2
try:
尝试:
SELECT * FROM weblinks WHERE catid = 4 AND ((title like 'A%') OR (title like 'B%'))
so on and so forth
等等等等
#5
1
SELECT * FROM weblinks WHERE catid = 4 AND title LIKE 'A%'
SELECT * FROM weblinks WHERE catid = 4 AND title LIKE'A%'
% tells "anything", so it's "A" then anything. Only works with the LIKE comparison operator.
%告诉“任何东西”,所以它是“A”然后是任何东西。仅适用于LIKE比较运算符。