MYSQL 5.6 : Select where value is in CSV colum - mysql

I have a database (Mysql 5.6) like this :
id | value
--------------------------
1 | "value1;value2;value3"
2 | "value4;value5;value6"
I'd like to make a request like :
SELECT id FROM table WHERE 'value5' IS IN value
return --> 2
SQLFiddle : http://sqlfiddle.com/#!9/b5c347
I cannot change the database schema

Use LIKE syntax, but use it like this:
SELECT id
FROM table
WHERE
value LIKE 'value5;%' OR
value LIKE '%;value5' OR
value LIKE '%;value5;%' OR
value LIKE 'value5';
This is a dirty solution, and not possible to index, but should fit your needs (On a smallish table in a reasonable amount of time)
Another solution (If you're sure your values don't contain commas):
SELECT id
FROM table
WHERE
FIND_IN_SET('value5', REPLACE(value, ';', ',')

If all the values are different, you might be able to use LIKE:
SELECT id FROM table WHERE value LIKE '%value5%';
Just keep in mind that it won't work in case you have some value that contains the other, like
value2;value3;value56
Or use a RegExp, like this:
SELECT id FROM table WHERE value REGEXP '(^|[^a-z0-9])value5([^a-z0-9]|$)'

Related

how to fetch data from json array field in sql

I am using node.js with squel.js for SQL database.
I am storing array of string into one table field.
Let us say, My table name is like XYZ.
And my table data is like
id col2
1 '["a", "b", "x", "y"]'
2 '["x", "y", "q"]'
3 '["q", "e"]'
4 '["p", "q"]'
Now i want to know how to make query for search like
if i want to how to make query if i want to fetch records like
If i want to fetch all records which contains like ["x", "y"]. In this scenario i want expect result id 1 and 2.
If i want to fetch all records which contains like ["q"]. In this scenario i want expect result 2 and 4.
Please help me here i stuck here. Thanks in advance.
If I understand your question correctly,just use LIKE can do it
For the first requirement,you can use
SELECT * FROM yourtable WHERE col2 LIKE `%"x"%` AND col2 LIKE `%"y"%`
OR
SELECT * FROM yourtable WHERE col2 LIKE `%"x","y"%`
Since I do not know the format exactly,is it just check match x and y? Or just match "x","y" exactly?
For the second requirement,you can use
SELECT * FROM yourtable WHERE col2 LIKE `%"q"%`
col2 can be a JSON column, thus you can do something like:
SELECT * FROM XYZ WHERE JSON_CONTAINS(col2, JSON_ARRAY('x', 'y'));
SELECT * FROM XYZ WHERE JSON_CONTAINS(col2, JSON_ARRAY('q'));
The second query would returns rows 2, 3 and 4, but I guess your expectation might be incorrectly stated, since 'q' exists in the col2 array for all of those.

MySQL using like to match specific string

In my column I can have either a string like : "data+" or "data+data+data+..(undefined times)..+"
I simply need to get the column where I have multiples data and not only one.
I tried with
mycol NOT LIKE '%+'
But it didn't work...
Actually I don't know the data it is a string that varies : 'blabla' or 'aString' or 'whatever'
If I had in my columns
'jdsjpgsg+jdsjpgsg+', 'dvgff+', 'ffef+eefds+ghghgh+'
I want to select only
'jdsjpgsg+jdsjpgsg+',
'ffef+eefds+ghghgh+',
NOT 'dvgff+' !
if you want to search '..xxx+..' then you should be use xxx+%
if you want to search '..+xxx..' then you should be use %+xxx
if you want to search '..++..' then you should be use %++%
if you want to search '..+..+..' then you should be use %+%+%
It is what I get too and I dont want that. It is actually what i don't want to select. If I had jdsjpgsg+jdsjpgsg+ in my table I want to select it and NOT jdsjpgsg+ It is tricky...
so you can try like '%+%+%' to exclude just one '+'
CREATE TABLE TestTable
(`text` varchar(90))
;
INSERT INTO TestTable
(`text`)
VALUES
('jdsjpgsg+jdsjpgsg+'),
('dvgff+'),
('ffef+eefds+ghghgh+')
;
select * from TestTable
where text like '%+%+%'
| text |
|--------------------|
| jdsjpgsg+jdsjpgsg+ |
| ffef+eefds+ghghgh+ |
SQL Fiddle Demo Link
The % is the wildcard character. You should be using the % after data. Try like:
SELECT * FROM `table` WHERE `mycol` NOT LIKE 'data+%'
The above query will filter out all the records that have any characters after data+.

SQL request with variable equal to current row column value in WHERE clause

Hi, just registered to ask this since I did not find answers after some research :
My table my_table has several columns, but only two matter here : id (integer, primary key, ...) and children (varchar that contains one or several {"number":"number"} as seen in the example below)
id | children
0 | {"0":"0"}
1 | {"1":"1"} {"2":"2"} {"3":"3"}
2 | {"2":"2"}
3 | {"3":"3"}
4 | {"4":"4"}
5 | {"5":"5"} {"6":"6"}
6 | {"6":"6"}
You can see that for any choosen row, the column children will always contain at least one occurence of {"number":"number"} where 'number' is equal to the value of column id of this row.
Sometimes it contains more than one occurence of {"number":"number"} with numbers of other rows' id.
I would like to build a SQL query that returns all rows :
where there is only one occurence of {"number":"number"} in the children column
and for each row, verify that the value 'number' in {"number":"number"} is equal to the row's id value.
I tried :
SELECT * FROM my_table
WHERE children=CONCAT('{"', my_table.id, '":"', my_table.id, '"}')
This returns nothing obviously...
I'm still searching but I guess some more experienced users will have a solution :)
EDIT1 : I wrote '=' instead of ':' in my query and never noticed it xD Thank you. The query is now correct and working as intended.
Select all records with an ID mismatch (you had an equal sign where it must be a colon):
SELECT *
FROM my_table
WHERE children NOT LIKE CONCAT('%{"', my_table.id, '":"', my_table.id, '"}%');
Select all records with only one pair:
SELECT *
FROM my_table
WHERE children NOT LIKE '{%{%';
Combine the two somehow if you want a cobined result :-)
I agree with very bad db design notice.
your problem is maybe very simple - see the change in concat function
SELECT * FROM my_table
WHERE children=CONCAT('{"', my_table.id, '":"', my_table.id, '"}')
Your method should work. There must be some other characters that you are missing.
Here is another method:
where children like '%{%' and
children not like '%{%{%' and
children like concat('%"', id, '":%') and
children like concat('%:"', id, '"%')
This is just comparing each section of the pattern.
You might also try:
WHERE children LIKE CONCAT('%{"', my_table.id, '":"', my_table.id, '"}%') AND
children NOT LIKE '%{%{%';
The most likely problem are spaces (or other characters) at the beginning or end of the string. This allows those, but doesn't allow multiple items in the list.

combining AND operator in mysql

i want to combine conditions by AND operator ..
in my query i want to combine 3 coditions where the result appears when all of them are true.
i want to achive this
SELECT * FROM table WHERE 'condition1' AND ('condition2' AND 'condition3');
i tried this combination but it doesnt work.
this is the actual code
SELECT * FROM planmenu WHERE name LIKE '%$search%' AND type LIKE '%$type%' AND dishcontent LIKE '%$dishcontent%'
where
$search, $type and $dishcontent are php variables. however when i place OR insteade of the second AND it works perfectly. but when i use AND it does not !!
There's nothing wrong with your SQL. Check your data.
create table planmenu (
name varchar(20),
type varchar(20),
dishcontent varchar(20)
);
insert into planmenu values ('a','b','c');
Now if you do this:
SELECT *
FROM planmenu
WHERE name LIKE '%z%'
AND type LIKE '%z%'
AND dishcontent LIKE '%c%';
You'll get zero records back because there are no records matching all three conditions.
If you do this:
SELECT *
FROM planmenu
WHERE name LIKE '%z%'
AND type LIKE '%z%'
OR dishcontent LIKE '%c%';
You'll get a record back because the OR makes it so that the first two conditions OR only the third condition has to be true. It is the equivalent of running this:
SELECT *
FROM planmenu
WHERE (name LIKE '%z%' AND type LIKE '%z%')
OR dishcontent LIKE '%c%';
Check your data. You don't have any records matching all three conditions.
Change the condition to the columns you want to query against, with the values you are trying to get.
SELECT
*
FROM table
WHERE
condition1='something' AND
condition2='somethingelse' AND
condition3='somethingdifferent';

MySQL How to Select only columns where value contains string

I did some searching and from one question already posted on stackexchange, the answer was that it was not possible, but I figured to ask. I did not know if it was possible to form a SELECT query to dynamically select which columns will be displayed in a mysql SELECT statement result. Example:
Say I have column names Person, ID, Phone Number, Alt Number for this table:
John | 79 | 800-499-0000 | 800-499-5555
I would like to form a SELECT statement so that it will only pull down columns where string '800-499' is somewhere in the field. Thus the result from MySQL ideally would be:
800-499-0000 | 800-499-5555
The only problem is that I do not think dynamically selecting columns is possible.
Any help or confirmation is appreciated.
You could try something like:
select * from
(select concat(case when col1 like '%800-499-0000%' then concat('col1: ',col1,';') end,
case when col2 like '%800-499-0000%' then concat('col2: ',col1,';') end,
...
case when coln like '%800-499-0000%' then concat('coln: ',coln,';') end)
as search_results
from my_table) sq
where search_results is not null