Php MySQL操作员选择如何从相似的行中获得一个精确的行结果?
我在简单的表类别(id、名称、SEO)中有这样的“SEO”行:Php MySQL操作员选择如何从相似的行中获得一个精确的行结果?,php,mysql,Php,Mysql,我在简单的表类别(id、名称、SEO)中有这样的“SEO”行: "abc" "abc 22" "abc 33" 在MySQL中如何使用此查询 SELECT id FROM categories WHERE seo='abc' AND LENGTH('abc')='3';" 从“abc”中得到一个精确的行结果“abc”,但没有找到“abc 22”和“abc 33”?因为您使用的是精确比较运算符,所以您可以使用: 从seo='abc'的类别中选择id 因为您使用的是=,所以不需要测试长度 cre
"abc"
"abc 22"
"abc 33"
在MySQL中如何使用此查询
SELECT id FROM categories WHERE seo='abc' AND LENGTH('abc')='3';"
从“abc”中得到一个精确的行结果“abc”,但没有找到“abc 22”和“abc 33”?因为您使用的是精确比较运算符,所以您可以使用:
从seo='abc'的类别中选择id代码>
因为您使用的是=
,所以不需要测试长度
create table stuff
( id int auto_increment primary key,
seo varchar(100) not null
-- unique key (seo) not a bad idea
);
insert stuff (seo) values ('abc'),('abc7'),('kitty likes abc'),('abc and more abc'),('kittens');
insert stuff (seo) values ('abc at beginning'),('frogs'),('and at the end abc'),('qwertyabcqwerty');
select id from stuff where seo='abc';
+----+
| id |
+----+
| 1 |
+----+
1 row in set (0.02 sec)
以下是like
的行为方式:
select * from stuff where seo like '%abc';
+----+--------------------+
| id | seo |
+----+--------------------+
| 1 | abc |
| 3 | kitty likes abc |
| 4 | abc and more abc |
| 8 | and at the end abc |
+----+--------------------+
4 rows in set (0.00 sec)
select * from stuff where seo like 'abc%';
+----+------------------+
| id | seo |
+----+------------------+
| 1 | abc |
| 2 | abc7 |
| 4 | abc and more abc |
| 6 | abc at beginning |
+----+------------------+
4 rows in set (0.00 sec)
select id,seo from stuff where seo like '%abc%';
+----+--------------------+
| id | seo |
+----+--------------------+
| 1 | abc |
| 2 | abc7 |
| 3 | kitty likes abc |
| 4 | abc and more abc |
| 6 | abc at beginning |
| 8 | and at the end abc |
| 9 | qwertyabcqwerty |
+----+--------------------+
7 rows in set (0.00 sec)
这应该在没有长度(…)的情况下工作,因为seo='abc'将检查字符串是否完全是seo。这是一个示例,没有长度,我得到3行结果,这与您实际的选择有关。我不知道你在运行哪个MySQL版本,但我只是测试了一下,有没有和LENGTH('abc')='3'代码>第一部分只得到一个结果。所有的教程和文档都提供了相同的信息:=表示完全相同。由于abc 22
或abc 33
不等于abc,因此该行不应出现在您的结果中。您好,我结束问题,只是分析了表格结果