Mysql - How to Use Like on Multiple Columns

MySQL - Using LIKE ? With Multiple Columns Search

You need to repeat the LIKE expression for each column.

$sql = "SELECT location 
FROM location_data
WHERE location LIKE ? OR CRS LIKE ? OR TIPLOC LIKE ?
LIMIT 10";

And since there are now 3 placeholders in the query, you need to fill them all in with the binding:

mysqli_stmt_bind_param($stmt, "sss", $param_term, $param_term, $param_term);

Using OR in LIKE Query in MySQL to compare multiple fields

Use this::

SELECT * FROM MyTable WHERE (Column1 LIKE '%keyword1%' OR Column2 LIKE 
'%keyword1%') AND (Column1 LIKE '%keyword2%' OR Column2 LIKE '%keyword2%');

How to search LIKE string value in multiple columns?

This (python) bit gives me the results I want.

    columns = ["col1", "col2", "col3", "col4", "col5", "col6", "col7", "col8"]
sql = "SELECT * FROM table WHERE col0 LIKE '%"+_name+"%'"
for column in columns:
sql += "OR "+column+" LIKE '%"+_name+"%s'"
sql += ";"

There must be a better way but this works for now.

mysql: Selecting multiple columns using LIKE

We can do this using dynamic MySQL. From the MySQL command line:

SELECT @cols := GROUP_CONCAT(column_name) FROM information_schema.columns
WHERE table_name = 'loc_tbl' AND column_name LIKE 'rp%';
SET @query = CONCAT("SELECT ", @cols, " FROM lob_tbl");
PREPARE stmt FROM @query;
EXECUTE stmt;

SQL WHERE statement multiple columns LIKE one value

The specific answer to your question is something like this:

WHERE CONCAT_WS('|', table1.email, table2.email) LIKE '%example@example.com%'

That would generally not be used. The more common approach is simply:

WHERE table1.email LIKE '%example@example.com%' OR
table2.email LIKE '%example@example.com%'

Using NOT LIKE on multiple columns

Just use not like:

SELECT *
FROM documents
WHERE (content LIKE '%office%' or notes LIKE '%office%') AND
(content NOT LIKE '%sweden%' and notes NOT LIKE '%sweden%');

Or if you prefer:

SELECT *
FROM documents
WHERE (content LIKE '%office%' or notes LIKE '%office%') AND
NOT (content LIKE '%sweden%' or notes LIKE '%sweden%');

One caveat: NOT LIKE ignores NULL values just as LIKE does. So if the values can be NULL, you need to take that into account.

SQL Query with one column and multiple LIKEs

You need to split the string. So,it will divide Driver|Helper|Garden to separate Driver, Helper and Garder columns.

This function is absent in mysql. You have to create it your own.
Check this link: https://blog.fedecarg.com/2009/02/22/mysql-split-string-function/


Also, you may work with SUBSTRING_INDEX function:

mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2);
-> 'www.mysql'
mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', -2);
-> 'mysql.com'

Codeigniter 3 use of db->like for multiple columns results in an SQL related error

In the new CodeIgniter release (3.1.9), there was a fix for an unwanted typo ... Inside system/database/DB_query_builder.php at line #973 I believe:

This :

case 'before':
$v = "%'{$v}'"; // <- Check here
break;

Was changed to this:

case 'before':
$v = "'%{$v}'"; // <- The fix
break;

Try to do this change and see if this fixes your issue or not.

EDITED

After seeing your code, please don't use * as a field name, you have to specify what field is used for the search ... For instance, if you want to search in titles, descriptions and contents, you may proceed as this:

$query = $this->db->like('title', $expression)
->or_like('description', $expression)
->or_like('content', $expression);

Then the rest of your code. Give it a shot.



Related Topics



Leave a reply



Submit