How to pass a variable to a IN clause? - mysql

Lets say I have a SP that has a SELECT statements as follows,
SELECT product_id, product_price FROM product
WHERE product_type IN ('AA','BB','CC');
But data goes to that IN clause must be through a single variable that contains the string of values. Something link below
SELECT product_id, product_price FROM product
WHERE product_type IN (input_variables);
But its not working that way. Any idea how to do this?

Pass parameter value like this - 'AA,BB,CC'. Then, it is enough to use FIND_IN_SET function -
SELECT product_id, product_price
FROM product
WHERE FIND_IN_SET(product_type, param);

create a user-defined function that will convert the comma separated value into table, and by join this two can get the desired result.
for more

passing a string using a variable was a problem assume this solution
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `spTestListValues`(_list varchar(200))
BEGIN
SET #LIST=_list; -- assume this a paramter from the stored procedure
SELECT NULL AS Id, '' AS Description --insert null value to be used for list box population
UNION
(SELECT id, Description
FROM test_table
WHERE FIND_IN_SET(id,#LIST) ORDER BY Description ASC) ;
END
Calling the procedure from other query window
call `spTestListValues`('4,5,3'); --no paramter currently for test
output
ID Description
NUll
1 TEST1
4 TEST2
5 TEST3

Try using a prepared statement:
https://dev.mysql.com/doc/refman/8.0/en/sql-prepared-statements.html

Related

Convert a string of comma separated values into values for mysql stored procedure

I am writing a stored procedure for mysql. I am using xampp localhost with mariadb.
The stored procedure receives an integer ID, and finds the string based on the ID frem the database. This string contains a list of numbers, seperated by commas.
I need to use this string of numbers for the IN clause in my SQL query.
CREATE PROCEDURE Test(IN id INT)
BEGIN
DECLARE clist VARCHAR(255);
SELECT clists INTO clist
FROM camp
WHERE cid = id ;
Now clist, a varchar variable contans a list of values like 4,6,8 in a string.
SELECT DISTINCT email
FROM table
WHERE ( col1 IN (clist) OR col2 IN (clist)
so in mysql, the query becomes like this, and fetches only a single ID of 4.
SELECT DISTINCT email
FROM table
WHERE ( col1 IN ('4,6,8') OR col2 IN ('4,6,8')
I want it to become
SELECT DISTINCT email
FROM table
WHERE ( col1 IN (4,6,8) OR col2 IN (4,6,8)
My question is - How do I remove the single quotes from the string?
Thanks.
It might be simplest (nastiest) to use FIND_IN_SET
WHERE FIND_IN_SET(col1, clist) > 0 OR FIND_IN_SET(col2, clist) > 0

mysql procedure for insert with select from another table

I want to create a procedure that creates a course for a user this takes one parameter that is userid and the other value will be selected from tbl_chapter.there are 27 chapters that are going to be selected and 27 inserts will be executed .insert is going to be like INSERT INTO tbl_user_chapter(user_id,chapter_id) VALUEs (9 , 1),(9,2),(9,3),....
I want something like this:
CREATE PROCEDURE createCourse (IN userid_param int)
BEGIN
INSERT INTO tbl_user_chapter(tbl_user_chapter.user_id,tbl_user_chapter.chapter_id) VALUE(userid_param , SELECT id FROM tbl_chapter)
END
SELECT id FROM tbl_chapter will be multiple rows.
I know this is wrong and I need help.
if there is a better way to do this please let me know.
If the select does not return one row, then don't use the VALUES( ) syntax. Use INSERT ... SELECT syntax:
CREATE PROCEDURE createCourse (IN userid int)
BEGIN
INSERT INTO tbl_user_chapter(user_id,chapter_id)
SELECT userid, id FROM tbl_chapter;
END
Make sure userid does NOT conflict with a column of the same name in your tbl_chapter table. If a column exists with that name, you should change the IN parameter of the stored procedure.

How to use concated variable into mysql query inside mysql stored procedure?

I have a concat string in mysql stored procedure. And I want to use that string in where clause in same stored procedure. But when I'm using it, I'm getting only first value as it is comma separated string (1,2,3). So I can't understand why this happens and what is solution for it. If anyone knows the reason and solution, then answer will be appreciated. Here is code of stored procedure.
DECLARE newids VARCHAR(255);
SELECT GROUP_CONCAT(id) INTO newids FROM temptblnewcustomers;
DROP TABLE IF EXISTS temptblcustomerspurchase;
CREATE TABLE temptblcustomerspurchase (b_id BIGINT(20), b_customer BIGINT(20), a_item VARCHAR(50));
INSERT INTO temptblcustomerspurchase(b_id, b_customer, a_item)
SELECT b.id AS b_id, b.customer_id AS b_customer, a.item AS a_item FROM purchases a JOIN (
SELECT id, customer_id, item FROM (
SELECT id, customer_id, item FROM purchases WHERE customer_id IN (newids) ORDER BY customer_id, id DESC) t
GROUP BY customer_id) b
ON a.item=b.item;
Here, temptblnewcustomers consists only one column with five rows having value like any id. And also in newids, I'm geting values like 2301,2302,2303. But when I'm using newids into WHERE IN (), It takes onle first id i.e, 2301. I think that there may be reason of newids datatype as it is VARCHAR and id of temptblnewcustomers is BIGINT. But I'm not sure that this can affect.
You can use Select query directly in WHERE IN Clause.

Count for separated by special symbol String in each row without using any other table or Stored Procedure

I have to get count of each value separated by ## without
using Any table or stored procedure . i have to get count for each value repeated by
special symbol as per the given schema. I have tried alot but i want to make this result set without helping any reference table or stored procedure or temporary table.
Schema is like this :-
CREATE TABLE USER_TABLE (
id INT,
name VARCHAR(20));
INSERT INTO USER_TABLE VALUES
(1, 'A##B##C'),
(2, 'B##C'),
(3,'A'),
(4,'B##C');
Expected output is Like :-
Name COUNT
A 2
B 3
C 3
Try these query, may help you:
Select GROUP_CONCAT((name) SEPARATOR '#') as name, count(name) as count FROM your_table

How to compare multiple parameters of a row column value?

how to write query for following request?
my table:
id designation
1 developer,tester,projectlead
1 developer
1 techlead
if id=1,designation="'developer'"
Then need to first,second records.Because 2 rows are having venkat.
if id=1,designation="'developer','techlead'" then need to get 3 records as result.
i wrote one service for inserting records to that table .so that i am maintaining one table to store all designation with same column with comas.
By using service if user pass id=1 designation="'developer','techlead'" then need to pull the above 3 records.so that i am maintaining only one table to save all designations
SP:
ALTER PROCEDURE [dbo].[usp_GetDevices]
#id INT,
#designation NVARCHAR (MAX)
AS
BEGIN
declare #idsplat varchar(MAX)
set #idsplat = #UserIds
create table #u1 (id1 varchar(MAX))
set #idsplat = 'insert #u1 select ' + replace(#idsplat, ',', ' union select ')
exec(#idsplat)
Select
id FROM dbo.DevicesList WHERE id=#id AND designation IN (select id1 from #u1)
END
You need to use the boolean operators AND and OR in conjunction with LIKE:
IF empid = 1 AND (empname LIKE '%venkat%' OR empname LIKE '%vasu%')
The above example will return all rows with empid equals 1 and empname containing venkat or vasu.
Apparently you need to create that query based on the input from user, this is just an example of how the finally query should look like.
Edit: Trying to do this within SqlServer can be quite hard so you should really change your approach on how you call the stored procedure. If you can't do this then you could try and split your designation parameter on , (the answers to this question show several ways of how to do this) and insert the values into a temporary table. Then you can JOIN on this temporary table with LIKE as described in this article.