How to search for value in array using mysql? - mysql

I have a column which stores values in array form like below
table_name : records
column_name : data sample
row 1 : ['John','Adam', 'Mike']
I am trying to apply query something like below
SELECT * FROM records WHERE data IN('Adam');
which is giving 0 results found.

The IN clause with one parameter is essentially the same as WHERE data = 'Adam', wich search for the exact value.
Since your row contains a different string from Adam, no results are returned.
The query for your situation should be something like this:
SELECT * FROM records WHERE data LIKE '%Adam%';

If you using JSONArray , you can try query using JSON_CONTAINS() function.
you can query like below:-
SELECT * FROM records WHERE JSON_CONTAINS(data, '[Adam]');
where data is column which contains the array.

Related

SQL SELECT everthing with value 5 but not specify from which column

I have tried to select something with SQL, and I've a problem with it.
What I want:
SQL SELECT * FROM table WHERE ? = '5';
Select everything which = 5, BUT not specify from which column.
Example:
From this ""database"", you should receive the 1st and the last row.
Is that possible?
You have to list the columns but you can use in. The where clause looks like:
where 5 in (price, height)
Note: This assumes that the columns have the same type. You could get type conversion errors if they are not.
Also, given the names of the column and the data, I assume that the columns are stored as numbers. Hence, I dropped the single quotes around 5. If they are really strings, then use the single quotes.
you need to add a condition to your query with or keyword so if any of them match the row will be shown as a result
SELECT * FROM tablename WHERE price =5 or height= 5
better you list your columns by name instead of using * after SELECT

SQL query for Checking value in an multi dimension array using WHERE IN clause

MY tables has entries in following format
ID DATA
1 {"rows":[{"unit":"10","price":"20"},{"unit":"20","price":"30"},{"unit":"30","price":"40"},{"unit":"40","price":"50"}],"table":"\n<table>\n<thead>\n<tr>\n<th>Unit</th>\n<th>Price</th>\n</tr>\n</thead>\n\n<tbody>\n\n<tr><td>10</td><td>20</td></tr><tr><td>20</td><td>30</td></tr><tr><td>30</td><td>40</td></tr><tr><td>40</td><td>50</td></tr>\n\n</tbody>\n\n</table>\n"}
I want entries where unit = 30.
I tried
SELECT * from content Where '30' IN "(".implode(',',`data`).");
but that returns an error. Please Help
How about:
SELECT * FROM content WHERE data LIKE '%"unit":"30"%';

sql query is not working in case of number is in decimal

I am using the following query, it is showing the empty result but the record exist in table.
Please let me know how can do it
select * from wp_rg_lead_detail where lead_id=5047 and field_number=1.6
select * from wp_rg_lead_detail where lead_id=5047 and field_number=1.6
in both case query return the empty result.but data exist in table.
data type of the field_number is float in database.
Change the column to to decimal or numeric,they store exact numeric data values.Floats are always approximative numbers(in the way that are stored)
EDIT:Try it like this
select * from wp_rg_lead_detail where lead_id=5047 and format(field_number,1)=1.6

can I pass a list in sql query to search

I want a sql query which serves to return a list of values of the inputted and get a returned list.
suppoze I have two fields in a table.
values [1-a,2-b,3-c,4-d], I pass in [a,c] how do I get [1,3]?
You need to iterate through the queries list and store the results in a results list. I dont think you can query a list of values at a time. One more approach is you can use 'where value = a or value = c'.
select * from tablename where columname1 in ('a','c');
This will give you your desired output-
select group_concat(col2) where col1 in ('a','c')

MySQL select with subquery having replace

So I have a data with format like ;1;;2; and then I need to use this number in a query so I thought I'd convert it to 1,2 and use that in a IN condition. In my table, the result should return 2 rows but instead it is returning only 1 row.
My query is like this. The subquery return 1,2 with no problem but only 1 row is retrieve.
select *
from wt_lists
where id IN ((select replace (replace(sendto, ';;',','),';','')
from wt_stats where statsid IN (1)))
But when I try it with this. It returns the correct result, which in my case is 2 rows.
select *
from wt_lists
where id IN (1,2)
What am I missing here?
Comma delimited strings need to be explicitly defined in the query in order to be used in the IN clause - there's countless examples on SO where people need to use dynamic SQL to incorporate user submitted comma delimited strings.
That said, I have a solution using the FIND_IN_SET function:
SELECT DISTINCT wl.*
FROM WT_LISTS wl
JOIN (SELECT REPLACE(REPLACE(ws.sendto, ';;',','),';','') AS ids
FROM WT_STATS ws
WHERE ws.statsid = 1) x ON FIND_IN_SET(wl.id, x.ids) > 0
You are replacing the string:
';1;;2;'
To:
'1,2'
So, you SQL query looks like:
select * from wt_lists where id IN ('1,2') from wt_stats where statsid IN (1)
To use IN clause you need select different values in different rows.
I found this store procedure that does exactly what you need.
http://kedar.nitty-witty.com/blog/mysql-stored-procedure-split-delimited-string-into-rows/
I have not tested, but it is the way.
Obs: Like David said in the comments above, parsing the data in your application is a better way to do this.