[英]How to use query string inside IN statement in MySQL stored procedure
friends I have a stored procedure. 朋友,我有一个存储过程。 which taking an input.
哪个输入。 But the input is a Query string.
但是输入是查询字符串。 When I'm executing that string in IN statement I'm not getting anything.
当我在IN语句中执行该字符串时,我什么也没得到。
My Stored Procedure is: 我的存储过程是:
CREATE DEFINER=`root`@`localhost` PROCEDURE `SampleProcedure`(IN category VARCHAR(255)
IN location VARCHAR(255),
IN classification VARCHAR(255))
BEGIN
SELECT u1.firstname , u1.lastname, u1.avatar , s1.address ,c1.cityName
FROM user u1,serviceprovider s1, city c1
WHERE s1.userId=u1.id
AND c1.cityId=s1.city
AND s1.serviceProviderId
IN
(SELECT DISTINCT serviceprovider_cl AS serviceProviderId FROM db.serviceprovider_classification t1
INNER JOIN
db.locationid t2 ON t1.serviceprovider_cl=t2.serviceprovider_locationId
INNER JOIN
db.serviceprovider_category t3 ON t2.serviceprovider_location
INNER JOIN
db.serviceprovider_category t3 ON t2.serviceprovider_locationId=t3.serviceprovider_category
WHERE
t1.serviceproviderclassification_classification IN (classification)
AND
t2.location_serviceLocation IN (location)
AND
t3.category_serviceProviderCategory IN (category)
);
END
In category, classification and location.
在类别,分类和位置中。 I'm getting another query in String.
我在字符串中得到另一个查询。 So to execute that string or How to convert it into query or how to use string as Query?
那么执行该字符串还是如何将其转换为查询或如何将字符串用作查询?
Thanks谢谢
for this you can use something called Prepared Statements, you can find more about that here ... 为此,您可以使用一种称为“ Prepared Statements”的东西,您可以在此处找到更多有关此的信息 ...
So here is an SQL Fiddle where you can see how prepared statement works... 因此,这是一个SQL Fiddle ,您可以在其中查看prepared语句的工作方式...
As you can see in this simple stored procedure it is not complicated that much. 如您所见,在这个简单的存储过程中并没有那么复杂。 Basically there is three step to do this.
基本上有三个步骤可以做到这一点。
First create string which will be used in prepared statement. 首先创建将在准备好的语句中使用的字符串。 You do this to connect your query and query you will get as a string (IN category VARCHAR(255)) into one statement.
您执行此操作以连接查询,并且查询将作为字符串(IN类别VARCHAR(255))进入一个语句。
In my Fiddle: 在我的小提琴中:
SET @myString =
CONCAT('SELECT * FROM t2 WHERE t1_id IN (', category, ')');
That is the hardest part. 那是最难的部分。 Than you should perapare statement from that string
比您应该从该字符串中扩展语句
PREPARE statement FROM @myString;
End than execute the statement 比执行语句结束
EXECUTE statement;
When you call your procedure you pass your string which will be part of statement: 当您调用过程时,您将传递字符串,该字符串将成为语句的一部分:
CALL SimpleProcedure('SELECT id FROM t1 WHERE val1 = "myVal2"');
And that's the logic you should apply on your problem. 这就是您应该对问题应用的逻辑。
That should look like this: 看起来应该像这样:
CREATE DEFINER=`root`@`localhost` PROCEDURE `SampleProcedure`(IN category VARCHAR(255))
BEGIN
SET @myString =
CONCAT('SELECT u1.firstname , u1.lastname, u1.avatar , s1.address ,c1.cityName
FROM user u1,serviceprovider s1, city c1
WHERE s1.userId=u1.id
AND c1.cityId=s1.city
AND s1.serviceProviderId
IN
(', category,
' INNER JOIN
db.serviceprovider_category t3 ON t2.serviceprovider_locationId=t3.serviceprovider_category
WHERE
t3.category_serviceProviderCategory IN (', category, '))');
PREPARE statement FROM @myString;
EXECUTE statement;
END
EDIT: note that between ' and INNER JOIN there is one blank space because CONCAT, without that, would connect last word from 'category' query and inner join and that will cause you problem and your query wont work! 编辑:请注意,在'和INNER JOIN之间有一个空格,因为CONCAT如果没有该空格,将连接'category'查询和内部联接的最后一个单词,这将导致您遇到问题,您的查询将无法正常工作!
GL! GL!
PS Also i notice that you mix both syntax when JOIN table (old comma separated JOIN and the new way) which is not look nice, it would be good to correct that and use new INNER JOIN syntax like you do in your sub query... PS另外我还注意到,当JOIN表(旧逗号分隔的JOIN和新方法)看起来不太好时,您会混合使用这两种语法,这将是更正此问题并像在子查询中一样使用新的INNER JOIN语法的好方法。 。
New EDIT (based on question edit) 新的EDIT (基于问题编辑)
CREATE DEFINER=`root`@`localhost` PROCEDURE `SampleProcedure`(IN category VARCHAR(255)
IN location VARCHAR(255),
IN classification VARCHAR(255))
BEGIN
SET @myString =
CONCAT('SELECT u1.firstname , u1.lastname, u1.avatar , s1.address ,c1.cityName
FROM user u1,serviceprovider s1, city c1
WHERE s1.userId=u1.id
AND c1.cityId=s1.city
AND s1.serviceProviderId
IN
(SELECT DISTINCT serviceprovider_cl AS serviceProviderId FROM db.serviceprovider_classification t1
INNER JOIN
db.locationid t2 ON t1.serviceprovider_cl=t2.serviceprovider_locationId
INNER JOIN
db.serviceprovider_category t3 ON t2.serviceprovider_location
INNER JOIN
db.serviceprovider_category t3 ON t2.serviceprovider_locationId=t3.serviceprovider_category
WHERE
t1.serviceproviderclassification_classification IN (', classification, ')
AND
t2.location_serviceLocation IN (', location, ')
AND
t3.category_serviceProviderCategory IN (', category, '))');
PREPARE statement FROM @myString;
EXECUTE statement;
END
Subquery in IN
statement is equal to JOIN
with subquery's table. IN
语句中的子查询等于与子查询表的JOIN
。 Why you need "exotic" syntax instead of simple join. 为什么需要“ exotic”语法而不是简单联接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.