我的存储过程是:
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
在类别,分类和位置。我在String中获得了另一个查询。那么要执行该字符串或如何将其转换为查询或如何使用字符串作为查询?
谢谢
答案 0 :(得分:1)
为此您可以使用名为Prepared Statements的内容,您可以找到有关here的更多信息......
所以这里是一个SQL Fiddle,你可以看到预备语句的工作原理......
正如您在这个简单的存储过程中所看到的那样,它并不复杂。基本上有三个步骤来做到这一点。
首先创建将在预准备语句中使用的字符串。这样做是为了将您的查询和查询连接到一个字符串(IN类别VARCHAR(255))中。
在我的小提琴中:
SET @myString =
CONCAT('SELECT * FROM t2 WHERE t1_id IN (', category, ')');
这是最难的部分。比你应该从那个字符串perapare语句
PREPARE statement FROM @myString;
结束执行语句
EXECUTE statement;
当你打电话给你的程序时,你传递的字符串将成为声明的一部分:
CALL SimpleProcedure('SELECT id FROM t1 WHERE val1 = "myVal2"');
这就是你应该对你的问题应用的逻辑。
这应该是这样的:
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
编辑:请注意'和INNER JOIN之间有一个空格,因为没有它的CONCAT会连接'category'查询和内连接中的最后一个单词,这会导致问题而你的查询无法正常工作!
GL!
P.S。另外我注意到你在JOIN表(旧逗号分隔JOIN和新方法)时混合两种语法,这看起来不太好,最好纠正它并使用新的INNER JOIN语法,就像你在子查询中一样...
新编辑(基于问题编辑)
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
答案 1 :(得分:0)
IN
语句中的子查询等于带有子查询表的JOIN
。为什么你需要"异国情调"语法而不是简单连接。