2016-11-10 24 views
0

如何在所有列上執行REGEXP?如何在表中的所有列上執行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) 

這是我的選擇所有列,並做了REGEXP 1列first_name

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';

回答

2

你可以把它寫每列或使用前的正則表達式CONCAT它。請回頭看看這一個:

SELECT * FROM datatables_demo where CONCAT(first_name, last_name, position, office) REGEXP 'T'; 
+0

tks,認爲這就是我要找的。 – HattrickNZ

+1

@HattrickNZ請注意,這將返回正則表達式匹配不同列的行。因此,如果正則表達式是'sj',它將匹配'first_name = james'和'last_name = jones',因爲連接會創建'jamesjones'。使用'CONCAT_WS()'在每列之間放置一個分隔符可能會更好。 – Barmar

相關問題