php select from mysql,其中标题以 A 开头(仅 A 开头)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2069186/
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-08-25 04:54:02  来源:igfitidea点击:

php select from mysql where title begins with A (and A alone)

phpmysqlselectalphabetical

提问by SoulieBaby

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:

我确定这非常简单,但似乎无法弄清楚。远的:

SELECT * FROM weblinks WHERE catid = 4 AND title LIKE 'A'

but returns nothing.. Could someone help me out with this?

但什么也没有返回..有人可以帮我解决这个问题吗?

Cheers

干杯

回答by Tyler Carter

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 的答案。

回答by Ignacio Vazquez-Abrams

The wildcards for LIKEare %and _, where % matches 0 or more characters and _ matches exactly one character.

通配符LIKE%and _,其中 % 匹配 0 个或多个字符,_ 匹配一个字符。

回答by Mark Byers

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]'

回答by catsby

try:

尝试:

SELECT * FROM weblinks WHERE catid = 4 AND ((title like 'A%') OR (title like 'B%'))

so on and so forth

等等等等

回答by zneak

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 比较运算符。