开发者

Is there a way to search all fields in a single mysql query? [duplicate]

开发者 https://www.devze.com 2023-02-21 15:34 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicates: MYSQL query for searching through ALL the fields?
This question already has answers here: Closed 11 years ago.

Possible Duplicates:

MYSQL query for searching through ALL the fields?

MySQL - Search in all fields from every table from a database

I have 28 fields in a table and one option I would like to give a user is to search every entry across all its fields. So Instead of writing something like this:

SELECT * FROM table WHERE field1 LIKE testfield OR field2 LIKE testfield OR field3 LIK开发者_如何学GoE testfield 
OR field4 LIKE testfield OR  ... etc

I would like to write something like this:

SELECT * FROM table WHERE * LIKE testfield;

This doesnt work so I wondering if there is another syntax or the feature doesnt exist?


There's no portable support for this type of query in SQL. Your best bet is to create another string/varchar column and concat the other columns into a string and then search on that.

SQL Server does support a contains keyword:

SELECT Name, Color FROM Production.Product
WHERE CONTAINS((Name, Color), 'Red');

But, if you want to be portable, you're better off with the extra column.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号