How to search mulitple value seperated by commas in mysql - php

How to search multiple values separated by commas.
ex:
table name : searchTest
id name keyword
1 trophy1 test1,test2,test3
2 trophy2 test2,test5
Points:
If i search for test2 both results trophy1 and trophy2 should be display.
If i search for trophy1 then trophy1 should be as result.
How to solve this issue.
thanks in advance

I would say that, here, your data structure is quite not right.
It would be a better solution to not store several values in one field using some comma-separated format, but use three tables, defined this way :
searchtest
id
name
keywords
id
word
keywords_searchtest
id_keyword
id_searchtest
With that, searching for entries in searchtest that have specific keywords would be as simple as :
select searchtest.*, keywords.*
from searchtest
inner join keywords_searchtest on keywords_searchtest.id_searchtest = searchtest.id
inner join keywords on keywords.id = keywords_searchtest.id_keyword
where keywords.word = 'test2'
And, additionnaly, you'd have to search for searchtest entries with a specific name :
select *
from searchtest
where name = 'trophy1'

These keywords must be stored in the separate table

For Point 1 :
select * from searchTest where keyword like LIKE '%test2%
For Point 2 :
select * from searchTest where name like LIKE 'trophy1%

Use the FIND_IN_SET MySQL string function, like so:
SELECT * FROM searchTest WHERE FIND_IN_SET('test2', keyword) > 0

You can use like.
select * from searchTest where keyword like '%test2%'
Where the % is a wildcard.

select * from searchTest where keyword LIKE 'test2' or LIKE '%,test2' or LIKE 'test2,%'
it will work i have done this using above query

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

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

MySQL Query- Issue while using like operator

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

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

I want to write a query which name having first character having a or b or c or c-g

I want to write a query which name having first character having a or b or c or c-g.
I have list of name with alphabetical order A-Z. i want to filter the by alphabetical order in 3 steps A-G, G-M, N-Z.
By clicking A-G the record shows the name which first character starting from A-G
mysql_query("select * from users where name like "A%" or name like "B%" or name like "C%" or name like "D%" or name like "E%" or name like "F%" or name like "G%"");
But i don't want to write the like several time
so is there any easy way instead of writing like several time.
You can try
mysql_query("select * from users where LOWER(SUBSTR(name, 1, 1)) IN ('a','b','c','d','e','f','g')");
REGEXP could work well here:
SELECT ........ WHERE `name` REGEXP '^[a-g]'
Alternatively, to make better use of indexes:
... WHERE `name` < 'G'
There are ways to achieve this:
First: you can do it with .. OR LIKE .. syntax, like you've described.
Second: use SUBSTRING() to pass into IN operator:
SELECT * FROM t WHERE SUBSTRING(name, 1,1) IN ('A', 'B', 'C')
or use ORD()
SELECT * FROM t WHERE ORD(SUBSTRING(name, 1,1)) BETWEEN ORD('A') AND ORD('C')
Third: use REGEXP:
SELECT * FROM t WHERE name REGEXP '^[ABC]'

Categories