如何在mysql中过滤包含特定字母的数据?

6tqwzwtp  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(241)

我有一张table pub ,如下表所示:

+------------+----------------------------------+
| pub_id     | pub_name                         |
+------------+----------------------------------+
| P0010101   | Jex Max Publication              |
| P0020101   | BPP Publication                  |
| H0030102   | New Harrold Publication          |
| H0040101   | Ultra Press Inc.                 |
| P0050101   | Mountain Publication             |
| P0060103   | Summer Night Publication         |
| P0070104   | Pieterson Grp. of Publishers     |
| P0080102   | Novel Publisher Ltd.             |
+--------+--------------------------------------+

我想过滤 AND 子句where if在列中 pub_id 包含 P0 在第一至第二个字母和 0101 在整个句子的第3到第6个字母处。
我试过:

SELECT pub_id, pub_name FROM pub WHERE LEFT(pub_id,2) = 'P0' and pub_id LIKE '%0101%'

但结果是这样的

+------------+----------------------------------+
| pub_id     | pub_name                         |
+------------+----------------------------------+
| P0010101   | Jex Max Publication              |
| P0020101   | BPP Publication                  |
| P0050101   | Mountain Publication             |
+--------+--------------------------------------+

我对结果的预期如下:

+------------+----------------------------------+
| pub_id     | pub_name                         |
+------------+----------------------------------+
| P0010101   | Jex Max Publication              |
+------------+----------------------------------+
00jrzges

00jrzges1#

你可以用 pub_id LIKE 'P00101%' . 你不需要 left() 那么。
文件 LIKE

相关问题