简体   繁体   中英

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. 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? I appreciate any help I can get.

Basics od mysql. Wildcard % tells db engine that we expect some characters before and after 'Peter' string.

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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