MySQL Query- Issue while using like operator - php

I have a table names and in that table there is a field info data is like this:
ID info
1 'alpha,romeo,ciera,delta'
2 'testing,temp,total'
I am trying to create a select query.
select * from names where info like '%$var%'
$var is data from php.
Problem is i want exact match. If i use above query and in $var if
data is rome then it also return row of romeo.
one more example-
data in table is testing,temp,total
user input data is test then it also return testing
i tried
select * from names where info like '$var%'
and
select * from names where info like '%$var'
but it didn't return data as i expected.
Please advise how can i achieve this.
Note- : This is an example i am not using mysql as its depreciated. I am using mysqli

Append , at begin and end of your target search string.
And then make sure your source string also has those ,
SQL Fiddle Demo
select *
from names
where concat( ',', info , ',') like
concat( '%,', $var, ',%')
The problem is this wont use any index. You should go for FULL TEXT search

Problem is i want exact match. If i use above query and in $var if data is rome then it also return row of romeo.
Don't use the LIKE operator, use exact match operator =
select * from names where info = '$var'

Use , in your query to use it as a delimiter and use multiple conditions to account for the "edge cases".
SELECT *
FROM names
WHERE
info LIKE '%,$var,%' OR
info LIKE '$var,%' OR
info LIKE '%,$var' OR
info = '$var'
If you have rows with info column:
alpha,romeo,ciera,delta
testing,temp,total
rome
foo,test,bar
berlin,paris,madrid,london,rome
venice,milano,rome,firence
black,crome
rome,fome,mome,kome,kome
Query with $var as "rome" will select:
rome
berlin,paris,madrid,london,rome
venice,milano,rome,firence
rome,fome,mome,kome,kome
But not:
alpha,romeo,ciera,delta
black,crome

Related

Find Numeric code string in a SELECT mysql statement with LIKE

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 '&#39'
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 "%'%");

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".

Match part of string (SQL LIKE) In MySQL

I've a field that looks like this
1,13,15
If I try to make a search to find all rows than contains "1", then it's taking all rows that have 1 inside, and not only rows that says 1, [something], but also "11","13" etc.
I've tried with a like statement and wildcards, but without luck.
If you're using MySQL, use FIND_IN_SET, not LIKE.
WHERE FIND_IN_SET('1', columnname)
But the best solution is to normalize your schema so you don't have comma-separated values in a column.
If you are using MySQL you can use regexp to check such values
where column_name regexp '^1,|,1,|,1$|^1$'
When you say "1*" it finds everything that has a one and anything after that. Just narrow down your search and serach for:
field LIKE "1,%" OR field LIKE "%,1,%" OR field LIKE "%,1" OR field = "1"
You can search for "1," OR "1" OR ", 1".
if your field is '1,13,15' change it to ',1,13,15,'
and your search to LIKE '%,1,%'
So depending on your db you should try something like this
SELECT *
FROM yourTable
WHERE ',' + yourField + ',' LIKE '%,' + #search + ',%'
Have you tried:
select rows
from table
where field contains '1,'
?
If you're using MS SQL Server, you should use LIKE '%1%'.

How to search for a comma-separated string in a database table in mySQL

How to search for a comma-separated string in a database table in mySQL.
Suppose, I have a string in variable $facilities='breakfast,dinner,lunch' and in my database I have a string saved in a field called facilities having values breakfast,dinner,clothing,lunch,hot water.
How do I get the row having values $facilities?
Work like this i hope this is your requirement
select * from table where field_name like '%$fieldname%'
You're going to need to break apart the comma separated string first, and include a clause for each, so for that input you'd generate SQL like (assuming you want rows that have all three, and maybe other flags set):
SELECT *
FROM YourTable
WHERE FIND_IN_SET('breakfast', facilities)
AND FIND_IN_SET('dinner', facilities)
AND FIND_IN_SET('lunch', facilities)
You can break the string apart in PHP with something a little like:
$facilities='breakfast,dinner,lunch';
$desired = explode ( "," , $facilities );
And just use a loop to build your SQL
SELECT *
FROM tableName
WHERE FIND_IN_SET('lunch', 'breakfast,dinner,lunch')
EDIT:
SELECT *
FROM tableName
WHERE FIND_IN_SET('lunch', food_column)
food_column is the name of the column where the string 'breakfast,dinner,lunch' is stored

Categories