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'
Related
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\"';
I have a column in my database name location which have city name and country name in this format New york,America. I want to run a select query with the explode function like which we use in php to separate values like this
so that i can get comma , separated values
select explode(",",location) from address;
Also with the alias of city column holding New York and alias of country holding value of America. So that i can use them in my store procedure and insert this values in references table in the columns city and country
You can not really "explode" or split on all comma's, but you can split a string on any comma using SUBSTRING_INDEX.
SELECT SUBSTRING_INDEX('New york,America', ',', 1);
-> New york
Use Group concat
SELECT GROUP_CONCAT( location )
FROM `address`
CREATE FUNCTION strSplit(x varchar(255), delim varchar(12), pos int)
returns varchar(255)
return replace(substring(
substring_index(x, delim, pos+1),
length(substring_index(x, delim, pos)) + 1
), delim, '');
select strSplit("aaa,b,cc,d", ',', 1) as second;
> b
You can also select the row and store it as a string, and use the explode function to separate the values if you are intending to use the function itself.
Assuming that exploding will be to create N number of rows, then you can do it like this.
SET #completeString = "Hola,New york,America,!,";
SET #divider = ",";
WITH RECURSIVE strings(m) AS (
SELECT
#completeString
UNION ALL
SELECT
SUBSTRING(m, INSTR(m, #divider)+ 1)
FROM
strings
WHERE
INSTR(m, #divider)!= 0
)
SELECT
SUBSTRING_INDEX(m, #divider, 1) exploted_strings
FROM
strings;
That'll give you five rows, including the empty string
+----------------+
|exploted_strings|
+----------------+
| Hola |
| New york |
| America |
| ! |
| |
+----------------+
Or have it in a Procedure.
CREATE PROCEDURE explode(IN completeString TEXT, IN divider TEXT)
BEGIN
WITH RECURSIVE strings(m) AS (
SELECT
completeString
UNION ALL
SELECT
SUBSTRING(m, INSTR(m, divider)+ 1)
FROM
strings
WHERE
INSTR(m, divider)!= 0
)
SELECT
SUBSTRING_INDEX(m, divider, 1)
FROM strings;
END
And call it like
CALL explode ("Hola,New york,America,!,",",");
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;
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
I have this table:
+---------+----------+
+ Items + Person +
+---------+----------+
+ 2,99,75 + Jack +
+ 4,9,63 + Rose +
+---------+----------+
Now I do a simple
LIKE :items
and binding it using
$stmt->bindParam(':items',$item,PDO::PARAM_STR);
where
$item = "%9%".
The result contains both Jack and Rose, which is wrong because I expected to have Rose only as my result. It seems that LIKE sees both 99 and 9. How can I restrict my LIKE to have only 9 because that was the value of $items?
The other answers are good to do. However, I pose this alternative based on the fact that Items appears to be ID's.
If you need to query off comma separated values I would recommend a separate table. Using LIKE to query in a single field will never truly be fool-proof and could be a security concern. Try this.
Table 1: Person
+---------+----------+
+ ID + Person +
+---------+----------+
+ <int> + <string> +
+---------+----------+
Table 2: Item
+---------+----------+
+ PersonID+ ItemID +
+---------+----------+
+ <int> + <int> +
+---------+----------+
Then use joins to query both tables as needed.
SELECT * FROM Person INNER JOIN Items ON Items.PersonID = Person.ID
WHERE Items.ItemID = '9';
This should provide you with every record in Person that has ItemID "9" associated with them.
Perhaps this might help: http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
Its because % represent one or more character (anything). So 99 will match the "%9%"
If you want only 9, you can try with
"%,9,%"
I believe the main issue on this is what #interrobang said, the way you are representing the data.
If this table X that you show is the list itens for each person, you should have a column with the person id and another column with the item id, and multiple lines to represent multiple itens for each person. Doing like this your search would be much faster and easier to use and mantain in the future.
SQL Fiddle
MySQL 5.5.30 Schema Setup:
CREATE TABLE person (
id int auto_increment primary key,
name varchar(20)
);
CREATE TABLE item (
id int auto_increment primary key,
name varchar(20)
);
CREATE TABLE person_item (
id int auto_increment primary key,
person_id int,
item_id int
);
ALTER TABLE person_item ADD UNIQUE (person_id,item_id);
INSERT INTO person(id,name) VALUES
(1, 'John'),
(2, 'Mary'),
(3, 'Oliver');
INSERT INTO item (id,name) VALUES
(1,'Pen'),
(2,'Pencil'),
(3,'Book');
INSERT INTO person_item (person_id,item_id) VALUES
(1,1),
(1,3),
(2,2),
(3,1);
Query 1:
select p.name from person_item pi, person p, item i
where pi.person_id = p.id
and pi.item_id = i.id
and i.name LIKE 'Book%'
Results:
| NAME |
--------
| John |