I want to find word in double quote in database column - php

I want to find some word on my database column with SQL command
e.g.
-------------------
| tb1 |
|-----------------|
| id |
| name |
-------------------
Records in name field
-> 1st Row "abc","aba","acc","bcc","aaa","bbb"
-> 2nd Row "abc","bcd","efc","aaa","sss","eee"
-> 3rd Row "acc","cdc","ass","qqq","sss","bbb"
how to find "acc" and "abc"

Use:
SELECT * FROM tb1 where name LIKE '%"abc"%';
Put string with double quotes "abc" inside '% .. %'.

If you want to select the rows from which the name field is equal to "acc" and "abc" then write
SELECT * FROM tb1 WHERE name LIKE '"acc"' OR name LIKE '"abc"';
Just in case if you want you can escape the double qoutes with \" not necessary though but this is how it would look like
SELECT * FROM tb1 WHERE name LIKE '\"acc\"' OR name LIKE '\"abc\"';
Or if you want just the name then write
SELECT name FROM tb1 WHERE name LIKE '\"acc\"' OR name LIKE '\"abc\"';

Related

How to check value in column which has comma separated value in Mysql? [duplicate]

I have a field COLORS (varchar(50)) in a my table SHIRTS that contains a comma delimited string such as 1,2,5,12,15,. Each number representing the available colors.
When running the query select * from shirts where colors like '%1%' to get all the red shirts (color=1), I also get the shirts whose color is grey (=12) and orange (=15).
How should I rewrite the query so that is selects ONLY the color 1 and not all colors containing the number 1?
The classic way would be to add commas to the left and right:
select * from shirts where CONCAT(',', colors, ',') like '%,1,%'
But find_in_set also works:
select * from shirts where find_in_set('1',colors) <> 0
FIND_IN_SET is your friend in this case
select * from shirts where FIND_IN_SET(1,colors)
Take a look at the FIND_IN_SET function for MySQL.
SELECT *
FROM shirts
WHERE FIND_IN_SET('1',colors) > 0
This will work for sure, and I actually tried it out:
lwdba#localhost (DB test) :: DROP TABLE IF EXISTS shirts;
Query OK, 0 rows affected (0.08 sec)
lwdba#localhost (DB test) :: CREATE TABLE shirts
-> (<BR>
-> id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> ticketnumber INT,
-> colors VARCHAR(30)
-> );<BR>
Query OK, 0 rows affected (0.19 sec)
lwdba#localhost (DB test) :: INSERT INTO shirts (ticketnumber,colors) VALUES
-> (32423,'1,2,5,12,15'),
-> (32424,'1,5,12,15,30'),
-> (32425,'2,5,11,15,28'),
-> (32426,'1,2,7,12,15'),
-> (32427,'2,4,8,12,15');
Query OK, 5 rows affected (0.06 sec)
Records: 5 Duplicates: 0 Warnings: 0
lwdba#localhost (DB test) :: SELECT * FROM shirts WHERE LOCATE(CONCAT(',', 1 ,','),CONCAT(',',colors,',')) > 0;
+----+--------------+--------------+
| id | ticketnumber | colors |
+----+--------------+--------------+
| 1 | 32423 | 1,2,5,12,15 |
| 2 | 32424 | 1,5,12,15,30 |
| 4 | 32426 | 1,2,7,12,15 |
+----+--------------+--------------+
3 rows in set (0.00 sec)
Give it a Try !!!
If the set of colors is more or less fixed, the most efficient and also most readable way would be to use string constants in your app and then use MySQL's SET type with FIND_IN_SET('red',colors) in your queries. When using the SET type with FIND_IN_SET, MySQL uses one integer to store all values and uses binary "and" operation to check for presence of values which is way more efficient than scanning a comma-separated string.
In SET('red','blue','green'), 'red' would be stored internally as 1, 'blue' would be stored internally as 2 and 'green' would be stored internally as 4. The value 'red,blue' would be stored as 3 (1|2) and 'red,green' as 5 (1|4).
select * from shirts where find_in_set('1',colors) <> 0
Works for me
If you're using MySQL, there is a method REGEXP that you can use...
http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp
So then you would use:
SELECT * FROM `shirts` WHERE `colors` REGEXP '\b1\b'
You should actually fix your database schema so that you have three tables:
shirt: shirt_id, shirt_name
color: color_id, color_name
shirtcolor: shirt_id, color_id
Then if you want to find all of the shirts that are red, you'd do a query like:
SELECT *
FROM shirt, color
WHERE color.color_name = 'red'
AND shirt.shirt_id = shirtcolor.shirt_id
AND color.color_id = shirtcolor.color_id
You can achieve this by following function.
Run following query to create function.
DELIMITER ||
CREATE FUNCTION `TOTAL_OCCURANCE`(`commastring` TEXT, `findme` VARCHAR(255)) RETURNS int(11)
NO SQL
-- SANI: First param is for comma separated string and 2nd for string to find.
return ROUND (
(
LENGTH(commastring)
- LENGTH( REPLACE ( commastring, findme, "") )
) / LENGTH(findme)
);
And call this function like this
msyql> select TOTAL_OCCURANCE('A,B,C,A,D,X,B,AB', 'A');
1. For MySQL:
SELECT FIND_IN_SET(5, columnname) AS result
FROM table
2.For Postgres SQL :
SELECT *
FROM TABLENAME f
WHERE 'searchvalue' = ANY (string_to_array(COLUMNNAME, ','))
Example
select *
from customer f
where '11' = ANY (string_to_array(customerids, ','))
All the answers are not really correct, try this:
select * from shirts where 1 IN (colors);

Search in a column sql by first and last characters

In my data table I have a column reference, data of this column is composed of categorie + idproduct + idmaker example:
reference | categorie + idproduct + idmaker
---------------|--------------------
cat48934547814 | [cat48][934][547814]
cat55548451412 | [cat55][548][451412]
cat48548547814 | [cat48][548][547814]
I want search all product having the reference that starts with cat48 and ends by 547814.
thanks
you could use a pair of like
select *
from my_table
where reference like 'cat48%'
and reference like '%547814'
or with substring
select *
from my_table
where left(reference, 5) = 'cat48'
and right(reference ,6) = '547814'

I can not get expected output by like operator, where field store multiple integer value [duplicate]

I have a field COLORS (varchar(50)) in a my table SHIRTS that contains a comma delimited string such as 1,2,5,12,15,. Each number representing the available colors.
When running the query select * from shirts where colors like '%1%' to get all the red shirts (color=1), I also get the shirts whose color is grey (=12) and orange (=15).
How should I rewrite the query so that is selects ONLY the color 1 and not all colors containing the number 1?
The classic way would be to add commas to the left and right:
select * from shirts where CONCAT(',', colors, ',') like '%,1,%'
But find_in_set also works:
select * from shirts where find_in_set('1',colors) <> 0
FIND_IN_SET is your friend in this case
select * from shirts where FIND_IN_SET(1,colors)
Take a look at the FIND_IN_SET function for MySQL.
SELECT *
FROM shirts
WHERE FIND_IN_SET('1',colors) > 0
This will work for sure, and I actually tried it out:
lwdba#localhost (DB test) :: DROP TABLE IF EXISTS shirts;
Query OK, 0 rows affected (0.08 sec)
lwdba#localhost (DB test) :: CREATE TABLE shirts
-> (<BR>
-> id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> ticketnumber INT,
-> colors VARCHAR(30)
-> );<BR>
Query OK, 0 rows affected (0.19 sec)
lwdba#localhost (DB test) :: INSERT INTO shirts (ticketnumber,colors) VALUES
-> (32423,'1,2,5,12,15'),
-> (32424,'1,5,12,15,30'),
-> (32425,'2,5,11,15,28'),
-> (32426,'1,2,7,12,15'),
-> (32427,'2,4,8,12,15');
Query OK, 5 rows affected (0.06 sec)
Records: 5 Duplicates: 0 Warnings: 0
lwdba#localhost (DB test) :: SELECT * FROM shirts WHERE LOCATE(CONCAT(',', 1 ,','),CONCAT(',',colors,',')) > 0;
+----+--------------+--------------+
| id | ticketnumber | colors |
+----+--------------+--------------+
| 1 | 32423 | 1,2,5,12,15 |
| 2 | 32424 | 1,5,12,15,30 |
| 4 | 32426 | 1,2,7,12,15 |
+----+--------------+--------------+
3 rows in set (0.00 sec)
Give it a Try !!!
If the set of colors is more or less fixed, the most efficient and also most readable way would be to use string constants in your app and then use MySQL's SET type with FIND_IN_SET('red',colors) in your queries. When using the SET type with FIND_IN_SET, MySQL uses one integer to store all values and uses binary "and" operation to check for presence of values which is way more efficient than scanning a comma-separated string.
In SET('red','blue','green'), 'red' would be stored internally as 1, 'blue' would be stored internally as 2 and 'green' would be stored internally as 4. The value 'red,blue' would be stored as 3 (1|2) and 'red,green' as 5 (1|4).
select * from shirts where find_in_set('1',colors) <> 0
Works for me
If you're using MySQL, there is a method REGEXP that you can use...
http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp
So then you would use:
SELECT * FROM `shirts` WHERE `colors` REGEXP '\b1\b'
You should actually fix your database schema so that you have three tables:
shirt: shirt_id, shirt_name
color: color_id, color_name
shirtcolor: shirt_id, color_id
Then if you want to find all of the shirts that are red, you'd do a query like:
SELECT *
FROM shirt, color
WHERE color.color_name = 'red'
AND shirt.shirt_id = shirtcolor.shirt_id
AND color.color_id = shirtcolor.color_id
You can achieve this by following function.
Run following query to create function.
DELIMITER ||
CREATE FUNCTION `TOTAL_OCCURANCE`(`commastring` TEXT, `findme` VARCHAR(255)) RETURNS int(11)
NO SQL
-- SANI: First param is for comma separated string and 2nd for string to find.
return ROUND (
(
LENGTH(commastring)
- LENGTH( REPLACE ( commastring, findme, "") )
) / LENGTH(findme)
);
And call this function like this
msyql> select TOTAL_OCCURANCE('A,B,C,A,D,X,B,AB', 'A');
1. For MySQL:
SELECT FIND_IN_SET(5, columnname) AS result
FROM table
2.For Postgres SQL :
SELECT *
FROM TABLENAME f
WHERE 'searchvalue' = ANY (string_to_array(COLUMNNAME, ','))
Example
select *
from customer f
where '11' = ANY (string_to_array(customerids, ','))
All the answers are not really correct, try this:
select * from shirts where 1 IN (colors);

select query issue in mysql

I have a mysql table below
ID Name
1 AAA
2 BBB
3 CCC
Now I have a variable containing a random value of column Name.
$name = "BBB"; //Now this value can be changed that means it can be CCC or AAA
I want the output to be
ID Name
2 BBB
1 AAA
3 CCC
If the value is CCC then the output should be like below. The row containing the value should be at the top and then rest of the rows.
ID Name
3 CCC
1 AAA
2 BBB
And as usual I am stuck with the select query. The normal select query doesn't work as it selects only one item.
$sql = "select * from table where name='".$name."'";
You can combine ORDER BY with CASE.
Something like:
SELECT * FROM your_table
ORDER BY
CASE WHEN name=? THEN 0
ELSE 1
END
Note that I have replaced injecting your php variable directly with a prepared statement (the question mark, PDO or mysqli).
In mysql it would be something like:
CASE WHEN name="' . mysql_real_escape_string($name) . '" THEN 0
Maybe you could try something like this:
SELECT * FROM table WHERE name='".$name."'
UNION (SELECT * FROM table WHERE name!='".$name."')
Using the FIELD( ) function in the ORDER BY clause you can achieve this. It works by specifying the column to sort by and then the values to sort in order. For example:
SELECT * FROM tablename ORDER BY FIELD(Name, 'CCC') DESC;

Find a string inside the column name using MySQL query

I'm trying to write a select query for searching string in the particular column.
For example :
+----+------+-------------------------------+
| id | name | alternate |
+----+------+-------------------------------+
| 1 | Test | Test,Tests,test,tests,te,test |
| 2 | Demo | Demo.demo,dem,Dem |
+----+------+-------------------------------+
etc...
I just want to write a query for checking the submitted value name in the column name alternate
For example : I will submit the name test and it should check exists or not with the column name alternate that contains
use FIND_IN_SET()
$query = SELECT * FROM tablename WHERE FIND_IN_SET('test', alternate);
if(mysql_num_rows($query) > 0){
//if test was found
} else {
// test not found
}
for more information here
Try this query in mysql:
SELECT ID,NAME,ALTERNATE,
CASE WHEN (FIND_IN_SET(name, alternate) > 0)
THEN 'TRUE' ELSE 'FALSE' END RESULT FROM TABLE1;
SQL Fiddle
try this
select count(*) as total from tablename where alternate LIKE %test%
if the total value returned is greater than zero then string test exist in alternate column
One way of doing it is using the String function INSTR to check whether the Substring exists in the String or not. You can even pass a variable in Substring and String to check.
SELECT IF(INSTR(alternate,'test')! = 0,'Exists', 'Not Exists') as SearchResult FROM table;
Documentation can be found at:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_instr

Categories