簡體   English   中英

MySQL運算符SELECT如何從類似的行中獲取一個確切的行結果?

[英]MySQL operator SELECT how-to get one exact row result from similiar rows?

我在簡單的表類別(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”?

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)

由於您使用的是精確比較運算符,因此您可以使用:

SELECT id FROM categories WHERE seo = 'abc';

由於您使用=因此無需測試長度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM