search without hyphen in mysql - php

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.

Related

mySQL: How do I combine a search value with a variable?

I'm sure that there is a stupidly simple solution to this, but unfortunately my google-fu is too weak to find it.
I have a number of different tables for sizing, all following the same naming convention i.e size_001, size_002 etc. Within a loop I need to get the size entry that matches with the results already found.
Unfortunately there are no totally unique identifiers, as they repeat in each table (roman numerals for sizing). But they are unique in each individual table. So what I've tried so far looks a little bit like this:
SELECT * FROM CONCAT('size_00', '.$sizeTableID[$j].') WHERE sizeName LIKE '$sizeNames[$j]'"
Where $sizeTableId is a number from 1-9 and sizeName is a string e.g II or VI or, occasionally (because there's no consisitency), 2 etc
I've also tried ''$var'' inside the CONCAT and not using the CONCAT at all. Really I just need a way to join the database.size_00 and an integer variable.
If I understand correctly, this is actually simple:
$tablename = 'size00'.$sizeTableID[$j];
$sql = "SELECT * FROM $tablename WHERE sizeName LIKE '{$sizeNames[$j]}'";
and I think that solves it.
PHP is a bit quirky here.....
Try this one (when the variable is from an array/object, surround it with {})
$sql = "SELECT * FROM CONCAT('size_00', '{$sizeTableID[$j]}') WHERE sizeName LIKE '{$sizeNames[$j]}'";

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.

mySql regular expression in WHERE statement

I have a database where I store a code which is an implode("|", $array) of various codes. So the results will be something like:
100|5100|510
100|5200|510
410|5200|520
100|790|5100|320
I want to write a regular expression to search mySQL for matches from PHP ... I might have something like 100|*|510 to show me both 100|5100|510 and 100|5200|510. This RegEx works in PHP:
/100\|(?:.*)\|510/
But it does not work in mySQL. I found answers that ?: does not work, so if I remove that and use
/100\|.*\|510/
i.e. Query is:
SELECT * FROM tra_amounts WHERE coa_codes REGEXP "/100\|.*\|510/"
It shows all results from the table.
How do I write a RegEx to match some parts of the code, while leaving other parts of the code as a wildcard?
Thanks!
Have you heard of MySQL's LIKE operator? Something like this might be what you have in mind:
SELECT *
FROM yourTable
WHERE someCol LIKE '100|%|510;
Assuming that someCol had 100|5100|510 and 100|5200|510 as data, this query would return both of these records.
Use RLIKE instead of LIKE or =.

How to find a specific word in a MySQL table?

i want to find specific keyword in MySQL Table ....
The main code is:
if ($_REQUEST["keyword"]<>'') {
$search_string = "AND trainer like '".mysql_real_escape_string($_REQUEST["keyword"])."%'
OR venue like '".mysql_real_escape_string($_REQUEST["keyword"])."%'
OR session like '".mysql_real_escape_string($_REQUEST["keyword"])."%'
OR course like '".mysql_real_escape_string($_REQUEST["keyword"])."%'
OR category like '".mysql_real_escape_string($_REQUEST["keyword"])."%'";
}
else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]."
WHERE id>0".$search_string;
}
The problem is the code above shows same result for any word searched. Any help of reference will be much appreciated.
For whole specific word you need means you have to go through with double %% before and after the word
if ($_REQUEST["keyword"]<>'') {
$search_string = "AND trainer like '%".mysql_real_escape_string($_REQUEST["keyword"])."%'
OR venue like '%".mysql_real_escape_string($_REQUEST["keyword"])."%'
OR session like '%".mysql_real_escape_string($_REQUEST["keyword"])."%'
OR course like '%".mysql_real_escape_string($_REQUEST["keyword"])."%'
OR category like '%".mysql_real_escape_string($_REQUEST["keyword"])."%'";
}
else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]."
WHERE id>0".$search_string;
}
Assuming your problem is that you're getting incorrect results, you can fix this with the following:
$search_string = " AND (trainer like '%".mysql_real_escape_string($_REQUEST["keyword"])."%'
OR venue like '%".mysql_real_escape_string($_REQUEST["keyword"])."%'
OR session like '%".mysql_real_escape_string($_REQUEST["keyword"])."%'
OR course like '%".mysql_real_escape_string($_REQUEST["keyword"])."%'
OR category like '%".mysql_real_escape_string($_REQUEST["keyword"])."%')";
Changes made:
Added brackets around your set of OR statements
Prepended the wildcard character to your search string, as well as the existing appended wildcard
Added a space before your AND so your resulting query doesn't end up containing id>0AND
Edit:
I thought I could help more by explaining what is going wrong. Assuming someone searches for 'foo' and your table is called 'bar', your code will generate the following query:
SELECT
*
FROM
bar
WHERE
id>0AND
trainer like 'foo%'
OR venue like 'foo%'
OR session like 'foo%'
OR course like 'foo%'
OR category like 'foo%'
There are a couple of problems here:
You have combined your parts incorrectly so AND winds up right next to 'id>0', a space before AND fixes that problem.
You haven't wrapped your set of OR statements in brackets, so you end up with a query that asks for ANY of the conditions to be met (including id > 0 by itself)
You have not prepended a wildcard character to the search string, meaning you will only find records that start with 'foo', and not records that contain 'foo' somewhere else.
The suggested changes above will fix these problems.

How to use LIKE operator against multiple values

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.

Categories