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.
Related
I save the single quote in my utf8 mysql database with ' .
now I just want to select all rows that include a ' in the name column. So I do:
SELECT * WHERE name LIKE "%'%"
but this will give me an empty result. if I do
SELECT * WHERE name LIKE "%&#%"
for example its working great.
What I am doing wrong? I want to search for the complete string '.
SELECT * FROM testone WHERE val LIKE '''
result
From Table
Here is a working example of your query:
https://www.db-fiddle.com/f/ci63XiAPbKLaz7bRksJ4gW/1
The problem may depend on something else, e.g. how the ' is stored in the database.
May be the case that the ' is inserted as is, so try this:
SELECT * FROM test WHERE name LIKE "%'%";
In the same db-fiddle you can try this.
You can perform the search for both cases in the same query:
SELECT * FROM test WHERE (name LIKE "%'%" OR name LIKE "%'%");
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.
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);
Using the following table "technotes":
And, using the following SQLi Query:
SELECT *
FROM
`technotes`
WHERE
`techgroup` LIKE '%dispatch%' OR
`techgroup` LIKE '%30243542%' AND
`expires` >= '2014-12-18' AND
`viewed` NOT LIKE '%30243542%'
What was expected was that alertid's: 23324325 & 23546576 (1st and last one) would be returned. But instead i am getting record 1,3,4,5 being returned. I am missing something in the order of operation or some other component in the SQLi select statement is malformed but not sure what. Can someone advise what I have left out or need to change so this works properly?
Try this:
SELECT * FROM
`technotes`
WHERE
(`techgroup` LIKE '%dispatch%' OR `techgroup` LIKE '%30243542%')
AND `expires` >= '2014-12-18'
AND (`viewed` NOT LIKE '%30243542%' OR ISNULL(`viewed`) )
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.