Match part of string (SQL LIKE) In MySQL - php

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%'.

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.

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.

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

codeigniter select as

how doHow do we do
select table.value as table_value from table in codeigniter?
The AS part doesnt work because when i try to access the value,
this doesnt work:
$qry_inp = 'select table.value as table_value from table '
$query = $this->db->query($qry_inp);
echo $query->row('table_value ');// this will be empty, but it shouldn`t be
doesn`t matter if its in AR or simple query
Pretty simple thing.
$this->db->select('COLUMN_ACTUAL_NAME as `COLUMN_NAME_YOU_WANT_TO_SHOW`');
i'm joining two tables in which column names are same so i separate both tables columns by using as keyword , this is how you can use AS in codeigniter
$this->db->select("departments.name AS 'dname'");
$this->db->select('positions.name');
Where is that behaviour documented? row doesn't take a column name as a parameter; it optionally takes a row number, and that's it. Access it like any other field:
echo $query->row()->table_value;

How to search mulitple value seperated by commas in mysql

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

Categories