Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Is it possible to use LIKE on all columns in a table?

I'm trying to make a simple search bar that searches through my database for certain words. It is possible to use the LIKE attribute without using WHERE? I want it to search all columns for the keywords, not just one. Currently I have this:

mysql_query("SELECT * FROM shoutbox WHERE name LIKE '%$search%' ")

Which obviously only searches for names with the search input. I tried both of these:

mysql_query("SELECT * FROM shoutbox LIKE '%$search%' ")
mysql_query("SELECT * FROM shoutbox WHERE * LIKE '%$search%' ")

and neither worked. Is this something that is possible or is there another way to go about it?

like image 382
user2566387 Avatar asked Aug 24 '13 06:08

user2566387


People also ask

How use multiple columns in SQL with like?

To select multiple columns from a table, simply separate the column names with commas! For example, this query selects two columns, name and birthdate , from the people table: SELECT name, birthdate FROM people; Sometimes, you may want to select all columns from a table.

How do you search for a string in all fields of every table in a MySQL database?

There is a Schemas tab on the side menu bar, click on the Schemas tab, then double click on a database to select the database you want to search. Then go to menu Database - Search Data, and enter the text you are searching for, click on Start Search.

Does like work in MySQL?

Does Ilike work in MySQL? It is in PostgreSQL the keyword ILIKE can be used instead of LIKE to make the match case-insensitive according to the active locale. This is not in the SQL standard but is a PostgreSQL extension. In MySQL you do not have ILIKE.

How can you list all columns for a given table in MySQL?

You can list a table's columns with the mysqlshow db_name tbl_name command. The DESCRIBE statement provides information similar to SHOW COLUMNS .


3 Answers

There's no shortcut. You need to specify each column separately.

SELECT * FROM shoutbox 
    WHERE name LIKE '%$search%' 
        OR foo LIKE '%$search%' 
        OR bar LIKE '%$search%'  
        OR baz LIKE '%$search%' 
like image 149
JJJ Avatar answered Oct 18 '22 21:10

JJJ


You might want to look at the MATCH() function as well eg:

SELECT * FROM shoutbox 
WHERE MATCH(`name`, `foo`, `bar`) AGAINST ('$search')

You can also add boolean mode to this:

SELECT * FROM shoutbox 
WHERE MATCH(`name`, `foo`, `bar`) AGAINST ('$search') IN BOOLEAN MODE

You can also get the relevance scores and add FULLTEXT keys to speed up the queries.

like image 28
Dave Avatar answered Oct 18 '22 22:10

Dave


One option is to use CONCAT to glue the columns together into a single string.

SELECT * FROM shoutbox 
WHERE CONCAT(name, foo, bar, baz) LIKE '%$search%' 

However, this method can occasionally produce false positives due to the columns getting glued together with no spacing. You can mitigate that problem by adding some kind of spacing (' '), delimiter ('-'), or null byte character ("\0") that you know won't occur in your search term.

For example, if the search term doesn't contain any spaces, then you can just delimit the columns with spaces:

SELECT * FROM shoutbox 
WHERE CONCAT(name, ' ', foo, ' ', bar, ' ', baz) LIKE '%$search%'

Whereas, if the search term does contain spaces, then a null byte would work better:

SELECT * FROM shoutbox 
WHERE CONCAT(name, "\0", foo, "\0", bar, "\0", baz) LIKE '%$search term%'
like image 8
esdiweb Avatar answered Oct 18 '22 22:10

esdiweb