php 从 mysql 中选择,其中标题以 A 开头(并且只有 A)

发布于 2024-08-17 13:36:55 字数 194 浏览 7 评论 0原文

我确信这非常简单,但似乎无法弄清楚。我需要从我的数据库中选择所有标题以 A、B 或 C 等开头的标题。这是我尝试过的远:

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

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

干杯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

皇甫轩 2024-08-24 13:36:55

对于以“A”开头的标题,请在 A 后使用 %

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

对于其中包含字母“A”的标题,请在两侧使用 % A

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

对于以字母“A”结尾的标题,请在 A 之前使用 %

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

基本上 % 是通配符。它告诉 MySQL 该位置中可以包含任何内容。

要将数字作为第一个字母,请查看 马克的回答。

For titles starting in 'A' use a % after the 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

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

For titles ending in the letter 'A', use % before the 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.

For having numbers as the first letter, check out Mark's answer.

泪之魂 2024-08-24 13:36:55

LIKE 的通配符为 %_,其中 % 匹配 0 个或多个字符,_ 恰好匹配 1 个字符。

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

梦幻的味道 2024-08-24 13:36:55

对于以 A 开头的现有答案是正确的:

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

对于以任何数字开头,您可以使用 REGEXP 运算符:

SELECT * FROM weblinks WHERE catid = 4 AND title REGEXP '^[0-9]'

The existing answers are correct for beginning with A:

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

For beginning with any number you can use the REGEXP operator:

SELECT * FROM weblinks WHERE catid = 4 AND title REGEXP '^[0-9]'
治碍 2024-08-24 13:36:55

尝试:

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

依此类推

try:

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

so on and so forth

你的他你的她 2024-08-24 13:36:55

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

% 表示“任何内容”,因此它是“A”然后是任何内容。仅适用于 LIKE 比较运算符。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文