如何对表中的所有列执行REGEXP?

时间:2016-11-10 22:59:55

标签: mysql

如何在所有列上执行REGEXP?

这是我的表:

mysql> select * from datatables_demo;
+----+------------+-----------+-------------+--------+------------+--------+
| id | first_name | last_name | position    | office | start_date | salary |
+----+------------+-----------+-------------+--------+------------+--------+
|  1 | Tiger      | Nixon     | Accountant  | Tokyo  | 2016-11-08 | 320800 |
|  2 | Garrett    | Winters   | Accountant2 | Tokyo  | 2016-11-08 | 170750 |
|  3 | Ashton     | Cox       | Accountant3 | Tokyo  | 2016-11-08 |  86000 |
|  4 | Cedric     | Kelly     | Accountant4 | Tokyo  | 2016-11-08 | 433060 |
+----+------------+-----------+-------------+--------+------------+--------+
4 rows in set (0.01 sec)

这是我选择所有列并在1列first_name上执行REGEXP的方法:

mysql> select * from datatables_demo WHERE first_name REGEXP 'T';
+----+------------+-----------+-------------+--------+------------+--------+
| id | first_name | last_name | position    | office | start_date | salary |
+----+------------+-----------+-------------+--------+------------+--------+
|  1 | Tiger      | Nixon     | Accountant  | Tokyo  | 2016-11-08 | 320800 |
|  2 | Garrett    | Winters   | Accountant2 | Tokyo  | 2016-11-08 | 170750 |
|  3 | Ashton     | Cox       | Accountant3 | Tokyo  | 2016-11-08 |  86000 |
+----+------------+-----------+-------------+--------+------------+--------+
3 rows in set (0.00 sec)

如何在所有列上执行REGEXP?这是我的尝试,但它不对。

mysql> select * from datatables_demo WHERE * REGEXP 'T';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* REGEXP 'T'' at line 1
mysql>

无法从docs

中解决

我是否必须按列写出REGEXP:
select * from datatables_demo WHERE column1 REGEXP 'T' OR column2 REGEXP 'T' OR columnN REGEXP 'T';

1 个答案:

答案 0 :(得分:2)

你可以按列编写它,也可以在regexp之前使用concat。请回顾一下:

i