How to use LIKE operator against multiple values - php

I have this query :
select * from users where mailaddress
NOT like '%banned_domain1.com%'
AND mailaddress NOT like '%banned_domain2.com%'
AND mailaddress NOT like '%banned_domain3.com%' ;
I want to make it more simple , I executed this query from command line :
select * from users where mailaddress
NOT like ('%banned_domain1.com%','%banned_domain2.com%','%banned_domain3.com%') ;
I got MySQL error :
ERROR 1241 (21000): Operand should contain 1 column(s)

You can use NOT REGEXP
SELECT * FROM users WHERE mailaddress NOT REGEXP 'banned_domain1.com|banned_domain2.com|banned_domain3.com';
See live demo

Instead of "Like" use "In" and format the email address like this:
select * from users where SUBSTR(mailaddress, INSTR(mailaddress, '#') + 1)
NOT IN ('banned_domain1.com','banned_domain2.com','banned_domain3.com');
The SUBSTR will remove the # and anything preceding it, leaving only the domain name then you can do a perfect comparison without wildcards using IN.
Cheers!

you have to mention the column every time
select * from tasks where title NOT LIKE '%eating lunch%' AND title NOT LIKE '%eating breakfast%' AND title NOT LIKE '%a new task%'
however as Bruno Domingues said use NOT IN that will be more easy

You cannot simplify your query. You need one LIKE per condition.

Related

SQL WHERE CONCAT LIKE NOT

I try to make SQL to search some string in database.
In this spesification, The SQL must be dont display one string in database.
my sql like this :
$query = "SELECT * FROM `chatuser` WHERE CONCAT( `fullname`,`image`) LIKE '%".$search_string."%' NOT (`$string is not be displayed`) " ;
is that possible ?
Thanks for help
The correct syntax of LIKE and NOT LIKE as two conditions would be:
SELECT * FROM chatuser
WHERE CONCAT(CustomerName,ContactName) LIKE '%t%'
AND CONCAT(CustomerName,ContactName) NOT LIKE '%m%';
You miss AND Between conditions. Also you have to repeat CONCAT(CustomerName,ContactName).
In the example above we are looking for all CustomerName+ContactName with a t in any place but if it doesn't have an m in any place.
From the docs found at https://www.w3resource.com/mysql/comparision-functions-and-operators/not-like.php
Example: MySQL NOT LIKE operator with (%) percent
The following MySQL statement excludes those rows from the table author, having the 1st character of aut_name ‘W’.
Code:
SELECT aut_name, country
FROM author
WHERE aut_name NOT LIKE 'W%';
And so it seems would work in your situation.

How to fetch value from table as per multiple id using MySQL

I need one help. I need to fetch value from table as per multiple ids which are in comma separated string using MySQL. I am explaining my table and query below.
db_basic:
id special name
1 2,3,4,5 Raj
2 4,2,5,6 Rahul
3 3,5,6 Rocky
My code is given below.
$special=2;
$qry=mysqli_query($connect,"select * from db_basic where special='".$special."'");
Here I need where special=2 is present inside that comma separated string those value will be fetched. I need only proper query. Please help me.
use below query
May be work for u
Use LIKE instead of "="
"select * from db_basic where special LIKE '%".$special."%'"
use FIND_IN_SET function for ex.
select * from db_basic where FIND_IN_SET(2,special)
Try this once,
select * from db_basic where FIND_IN_SET($special, special);
It should work.
if you current code expeted to return the first and second line of your db, you can use the LIKE operator in the query like this :
$special=2;
$qry=mysqli_query($connect,"select * from db_basic where special LIKE '%,".$special.",%'");
or LOCATE like :
$special=2;
$qry=mysqli_query($connect,"select * from db_basic where LOCATE(".$special.",special)>0");
And FIND_IN_SET : From the docco here - "This function does not work properly if the first argument contains a comma (“,”) character".

search without hyphen in mysql

Mysql database contains below kind of values :
'AE01-1056 Ricoh Aficio' OR 'Ricoh AE01-1087 (AE01-1069)' etc
AS if am a normal user i will search the product name in simple text like
AE011056 ... but the result is not found.
i hav tried this query:
$q="SELECT * FROM mytable WHERE (p.product_name LIKE '$name%' OR c.category_name LIKE '$name%' OR pm.name LIKE '$name%')";
What change should i make in my query to get the product , because i have tried LIKE operator & it's not working for me.
Use replace function
$q="SELECT * FROM mytable
WHERE (REPLACE(p.product_name,'-','') LIKE '%$name%'
OR REPLACE(c.category_name,'-','') LIKE '%$name%'
OR REPLACE(pm.name ,'-','') LIKE '%$name%')";
I think there are only two ways:
1. Manipulate search string
If you knwo, users are often search for a code and don't use nessesary hyphens, check the searchstring bevor searching if it follows a given format an insert the hypen if it is missing.
2. replace all hyphens in the where-statement
see http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_replace
Depending on your setup, solution one might be the more performant solution, since you only have to do one instead multiple stringmanipulations.

How to select rows by substring? Similar to Google Suggest [duplicate]

I have mysql table that has a column that stores xml as a string. I need to find all tuples where the xml column contains a given string of 6 characters. Nothing else matters--all I need to know is if this 6 character string is there or not.
So it probably doesn't matter that the text is formatted as xml.
Question: how can I search within mysql?
ie
SELECT * FROM items WHERE items.xml [contains the text '123456']
Is there a way I can use the LIKE operator to do this?
You could probably use the LIKE clause to do some simple string matching:
SELECT * FROM items WHERE items.xml LIKE '%123456%'
If you need more advanced functionality, take a look at MySQL's fulltext-search functions here: http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html
Using like might take longer time so use full_text_search:
SELECT * FROM items WHERE MATCH(items.xml) AGAINST ('your_search_word')
SELECT * FROM items WHERE `items.xml` LIKE '%123456%'
The % operator in LIKE means "anything can be here".
Why not use LIKE?
SELECT * FROM items WHERE items.xml LIKE '%123456%'
you mean:
SELECT * FROM items WHERE items.xml LIKE '%123456%'
When you are using the wordpress prepare line, the above solutions do not work. This is the solution I used:
$Table_Name = $wpdb->prefix.'tablename';
$SearchField = '%'. $YourVariable . '%';
$sql_query = $wpdb->prepare("SELECT * FROM $Table_Name WHERE ColumnName LIKE %s", $SearchField) ;
$rows = $wpdb->get_results($sql_query, ARRAY_A);

MySQL query to get user where email equals some variable

First of all i don't know where to start doing this. That's why i am posting this question. Maybe you guys go for down-vote or close or flag it.
Here is the problem.
<?php
$gmail = "dipesh#some.com"
$split = explode('#',$service['User']['email']);
?>
The code above outputs an array with two elements inside it: one for dipesh and other for some.com. That is correct as expected.
Now I want a MySQL query that will perform select query to get record for dipesh#.
Example
SELECT * FROM users WHERE users.email LIKE 'dipesh';
The code above returns all the records that contain dipesh, but I want only the ones that contain dipesh#.
Does SELECT * FROM users WHERE users.email LIKE '%dipesh'; work?
If it does then what is the difference between the two?
Thanks
Your PHP code seems unrelated to your problem, but it sounds like you want email = 'dipesh#', which matches dipesh# literally. If you want dipesh# followed by anything, use email LIKE 'dipesh#%'. The % is a wildcard meaning "match zero or more of any character."
try this
SELECT * FROM users WHERE users.email LIKE 'dipesh#';
and if you want to dipesh# followed by anything else
use this
SELECT * FROM users WHERE users.email LIKE 'dipesh#%';
You probably want to try this
"SELECT * FROM users WHERE users.email LIKE 'dipesh#%'";
Hope this helps
SELECT * FROM users WHERE email LIKE 'dipesh#%';

Categories