简体   繁体   English

如何检查Mysql行中的文本?

[英]How do I check Mysql row for text?

I'm trying to check if a row in my table contains a specific string. 我正在尝试检查表中的行是否包含特定的字符串。 If it does, I want to get the user who has the specific string. 如果是这样,我想获取具有特定字符串的用户。 My table looks like this: The picture 我的桌子看起来像这样: 图片

I want to check the String "Peter", in the Address column/row. 我想检查“地址”列/行中的字符串“ Peter”。 How do I scan through it, and find it? 我如何浏览并找到它? Then after doing that, how would I check for the ID of the user who owns the String? 然后,这样做之后,我将如何检查拥有字符串的用户的ID? I appreciate any help I can get. 感谢您能提供的任何帮助。

Basics od mysql. mysql的基础知识。 Wildcard % tells db engine that we expect some characters before and after 'Peter' string. 通配符%告诉db引擎,我们期望在'Peter'字符串之前和之后的一些字符。

SELECT * FROM tablename WHERE Address LIKE '%Peter%'

Matches: 火柴:

"Peter's address"
"It is Peter's address"
"Address of Peter"

编写SQL命令以扫描数据。针对您的问题的命令看起来像::

select * from Mytable where address="peter";

Some alternatives to LIKE given 给定LIKE的一些替代方案

MariaDB [BANK]> select * from customer;
+------+---------+-------+-----------+-------------+----------+--------+------------+------+--------------+-------+
| id   | version | title | FirstName | Middlenames | LastName | Gender | Dob        | Dod  | Warning_flag | Worth |
+------+---------+-------+-----------+-------------+----------+--------+------------+------+--------------+-------+
|    1 |    1.00 | Mr    | fname1    | NULL        | lname1   | m      | 1990-01-01 | NULL | NULL         | NULL  |
|    2 |    1.00 | Mrs   | fname1    | NULL        | lname1   | f      | 1990-01-01 | NULL | NULL         | NULL  |
|    3 |    1.00 | Mrs   | fname1    | NULL        | lname1   | f      | 1990-01-01 | NULL | NULL         | NULL  |
+------+---------+-------+-----------+-------------+----------+--------+------------+------+--------------+-------+
3 rows in set (0.00 sec)

MariaDB [BANK]> SELECT * FROM CUSTOMER
    -> WHERE INSTR(LASTNAME,'a') > 0;
+------+---------+-------+-----------+-------------+----------+--------+------------+------+--------------+-------+
| id   | version | title | FirstName | Middlenames | LastName | Gender | Dob        | Dod  | Warning_flag | Worth |
+------+---------+-------+-----------+-------------+----------+--------+------------+------+--------------+-------+
|    1 |    1.00 | Mr    | fname1    | NULL        | lname1   | m      | 1990-01-01 | NULL | NULL         | NULL  |
|    2 |    1.00 | Mrs   | fname1    | NULL        | lname1   | f      | 1990-01-01 | NULL | NULL         | NULL  |
|    3 |    1.00 | Mrs   | fname1    | NULL        | lname1   | f      | 1990-01-01 | NULL | NULL         | NULL  |
+------+---------+-------+-----------+-------------+----------+--------+------------+------+--------------+-------+
3 rows in set (0.00 sec)

MariaDB [BANK]>
MariaDB [BANK]> SELECT *
    -> from customer
    -> WHERE position('a' in lastname) > 0;
+------+---------+-------+-----------+-------------+----------+--------+------------+------+--------------+-------+
| id   | version | title | FirstName | Middlenames | LastName | Gender | Dob        | Dod  | Warning_flag | Worth |
+------+---------+-------+-----------+-------------+----------+--------+------------+------+--------------+-------+
|    1 |    1.00 | Mr    | fname1    | NULL        | lname1   | m      | 1990-01-01 | NULL | NULL         | NULL  |
|    2 |    1.00 | Mrs   | fname1    | NULL        | lname1   | f      | 1990-01-01 | NULL | NULL         | NULL  |
|    3 |    1.00 | Mrs   | fname1    | NULL        | lname1   | f      | 1990-01-01 | NULL | NULL         | NULL  |
+------+---------+-------+-----------+-------------+----------+--------+------------+------+--------------+-------+
3 rows in set (0.00 sec)

MariaDB [BANK]>
MariaDB [BANK]> SELECT *
    -> from customer
    -> WHERE locate('a',lastname)  > 0;
+------+---------+-------+-----------+-------------+----------+--------+------------+------+--------------+-------+
| id   | version | title | FirstName | Middlenames | LastName | Gender | Dob        | Dod  | Warning_flag | Worth |
+------+---------+-------+-----------+-------------+----------+--------+------------+------+--------------+-------+
|    1 |    1.00 | Mr    | fname1    | NULL        | lname1   | m      | 1990-01-01 | NULL | NULL         | NULL  |
|    2 |    1.00 | Mrs   | fname1    | NULL        | lname1   | f      | 1990-01-01 | NULL | NULL         | NULL  |
|    3 |    1.00 | Mrs   | fname1    | NULL        | lname1   | f      | 1990-01-01 | NULL | NULL         | NULL  |
+------+---------+-------+-----------+-------------+----------+--------+------------+------+--------------+-------+
3 rows in set (0.00 sec)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM