Passing comma delimited parameter to stored procedure in mysql - mysql

I have fields in mytable tab1:
col1 | col2 | id
--------------------------
1,2,3 | 2,3,4,5 | a1
6,7,8,9 | 2,9,11 | a2
i want to pass this fields to my stored procedure input like where col1 in ('1,2') and col2 in ('3,4');
but it is not working ..

Something like this should work:
SELECT t.* FROM tab1 t
WHERE 1 IN (t.col1) AND 2 IN (t.col1) ...
AND 3 IN (t.col2) AND 4 IN (t.col2) ...
You would need to build up your query based on the inputs to your stored procedure, but otherwise this should work for you. Post your current proc?
EDIT
The find_in_set function will also work for you, but you will still have to split your inputs and call it once per number passed to the proc (i.e. the first parameter to FIND_IN_SET cannot be a comma separated list). Reference here: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set

Related

How to select data from array stored as string in table in MySQL

My team has stored array data as a string in MySQL like below
["1","2","22","11"]
How can we select data from the table where the column contains a certain branch number.
Example of table
sno | Name | Branch
1. | Tom. | ["1","2","22"]
2. | Tim. | ["1","2"]
Can you suggest a query to select all rows containing branch 2?
We tried using FIND_IN_SET() but that is not working as the double quotes and square brackets are also a part of string.
Use like:
select *
from mytable
where Branch like '%"2"%'

SELECT and CONCAT in MySQL

I'm using MySQL and trying to use CONCAT_WS() function with SELECT.
I tried:
SELECT id, A_12, CONCAT_WS('A_', '12') as testA from TABLE_A
I expected something like
id | A_12 | testA |
-------------------
1 | 20 | 20 |
The value is testA should be same as the value in A_12.
However, what I'm getting is
id | A_12 | testA |
-------------------
1 | 20 | 12 |
The '12' in testA column is simply coming from latter string of the CONCAT_WS() function CONCAT_WS('A_','12').
Any help would be appreciated.
======EDIT======:
Sorry I didn't clearly state my question and purpose in the beginning. I have 12 columns A_1, A_2, ... , A_12 in TABLE_A. More specifically, Table_A looks like this:
id | A_1 | A_2 | ... | A_12|
---------------------------
1 | 4 | 5 | ... | 20 |
2 | 1 | 4 | ... | 50 |
3 | 2 | 5 | ... | 70 |
I also have another table TABLE_B that looks something like this:
id | value
----------
1 | 12
2 | 5
3 | 3
I'm trying to create a stored function that...
select the corresponding value from TABLE_B
from TABLE_A, pull info under the column A_ + the value from Table_B
for every id.
So I have
SELECT id, CONCAT_WS('A_', stored-value-from-TABLE_B) as testA from TABLE_A
To make sure if the code is running as I expect, I ran
SELECT id, A_12, CONCAT_WS('A_', '12') as testA from TABLE_A
since the value for id=1 in Table_B is 12.
However, what I'm getting is 12 in testA column for every id.
You can join the tables on their ids and pick the proper A_? with the function ELT() where you have to enumerate all 12 columns:
SELECT b.*,
ELT(b.value, a.A_1, a.A_2, ..., a.A_12) testA
FROM TABLE_B b INNER JOIN TABLE_A a
ON b.id = a.id;
See a simplified demo.
well to get this result just need
SELECT id, A_12, A_12 as testA from TABLE_A
You seem to think that a select-list is bound to be a list of column names, therefore a string function like CONCAT_WS() would produce a string like A_12 and that string must be interpreted as a column identifier, so the result of that query would use the value of the column named by that string.
But that's not how an SQL select-list works.
A select-list is a list of expressions. You could use a simple column name, and the result would be the value in that column. Or you could use another expression, in this case a string function, and the result will be the string returned by that function — not the column that coincidentally has a name matching that string.
As mentioned in the comment above, identifiers are fixed in an SQL query. You cannot make a string expression and have the value of that expression be interpreted as an identifier in the same query. To make a dynamic reference to an identifier, you need to format it in your SQL syntax before you prepare the query.
You also misunderstand what CONCAT_WS('A_', '12') does. It concatenates its 2nd argument and further arguments, with a separator between them of the 1st argument. A typical usage would be:
CONCAT_WS(', ', col1, col2, col3, ...)`
This returns a list of comma-separated words from the values of several columns: "value1, value2, value3".
So in your case, you concatenated a single value "12" but the separator "A_" does not appear because there is only one value in the list.
First, you don't want CONCAT_WS(), you want CONCAT() - Concat with separator inserts whatever your first argument is between all the others, and since you only have one other argument, it never gets used - for example CONCAT_WS('A_', '12', '13') would give you 12A_13. CONCAT('A_', '12') gives you A_12 but as a string, not as a column name.
After correcting to CONCAT and evaluating, your select will look like this SELECT id, A_12, 'A_12' as testA from TABLE_A; Notice the quotes around A_12.
This is because concat functions return a string and can't be used to build a column name in a select string the way you want. It's possible to do so, but is complicated - you would have to build your entire query string in a string variable then execute it as a prepared statement:
SET #QueryString = CONCAT('SELECT id, A_12, ', CONCAT('A_', '12'), ' as testA from TABLE_A;');
PREPARE stmnt FROM #QueryString;
EXECUTE stmnt;
The nested CONCAT() is unnecessary, since concat can take any number of arguments so you get it simplified to this:
SET #QueryString = CONCAT('SELECT id, A_12, ', 'A_', '12', ' as testA from TABLE_A;');
PREPARE stmnt FROM #QueryString;
EXECUTE stmnt;
And your #QueryString will be SELECT id, A_12, A_12 as testA from TABLE_A;
This can be pretty dangerous if anything in that string comes from user input in a system. If this is connected to any application, combine the strings using whatever concatenation your server side application language uses then execute that as a query.
DB Fiddle testing (SQLFiddle seems to have removed support for selects in prepared statements)

How to split column into multiple rows (with pipe as separator) in mysql?

I've table1 like this:
col_raw
| COL1 |COL2| COL3 | COL4 |
-------------------------------------
|0123456789|Male|Basketball|Aquarius|
-------------------------------------
I would like to convert table1 to table2 which is consist of this column header name
table2:
Column Names: Phone Number | Gender | Hoby | Zodiak
Let me know if you ever query like this in mysql. Thank you!
In case, you always have 4 columns, you can use the below
select substring_index(column_name,'|',1) firstcol,
substring_index(substring_index(column_name,'|',2),'|',-1) secondcol,
substring_index(substring_index(column_name,'|',3),'|',-1) thirdcol,
substring_index(substring_index(column_name,'|',4),'|',-1) fourthcol
from table_name;
To understand the above, you can try running the below query, you will see how the substring_index is being used multiple times in the above query to fetch the required details
select substring_index(column_name,'|',1),
substring_index(column_name,'|',2),
substring_index(column_name,'|',3),
substring_index(column_name,'|',4)
from table_name;

Select a particular value of JSON string using MySQL

I have a table that looks like this
+------+------------------------------------+
| id | details |
+------+------------------------------------+
| 1 | {"price":"24.99","currency":"USD"} |
+------+------------------------------------+
Is it possible to, with a single MySQL select statement, obtain the value of price 24.99?
Yes, you can using JSON_EXTRACT
It probably should be like:
SELECT JSON_EXTRACT(details, "$.price")
FROM table_name
or another form:
SELECT details->"$.price"
FROM table_name
(I don't have MySql to test it)
Note that the price in your JSON stored as a string, not a number and you probably would want to cast it to a DECIMAL.

mysql/oracle stored math formula

is there any way apply math formula from stored string in Oracle and or MySQL?
col1 | col2 | formula
---------------------
2 | 2 | col1*col2
2 | 3 | col1+col2
SELECT * from tbl
result:
col1 | col2 | formula
---------------------
2 | 2 | 4
2 | 3 | 5
edit: for each row another formula
I think what you're saying is you want to have the database parse the formula string. For example, for Oracle you could
Add a column to the table to contain the result
Run an update statement which would call a PL/SQL function with the values of the columns in the table and the text of the formula
update {table} set formula_result = fn_calc_result (col1, col2, formula_column);
The PL/SQL function would create a string by replacing the "col1" and "col2" and so forth with the actual values of those columns. You can do that with regular expresions, as long as the formulas are consistently written.
Then use
execute immediate 'select '||{formula}||' from dual' into v_return;
return v_return;
to calculate the result and return it.
Of course, you could also write your own parser. If you decide to go that way, don't forget to handle operation precedence, parentheses, and so forth.
I think you want a virtual column. See here for excellent article on its setup and use.
you may do it via a PL/SQL script that you can trigger automcatically when inserting the data.
See http://en.wikipedia.org/wiki/PL/SQL
PL/SQL is a kind of program that executes in the database itself. It's quite easy to do.