我们有一个很长的 sql 过程,它接受 limit & amount(limitCount) 参数。因此,我们使用 concat 语句来连接多个查询。当我们尝试运行它时,调用此过程会给出 err.no 1064。
编辑:根据评论,我添加了整个代码。
CREATE PROCEDURE getProfileTasks (IN p_id1 INT, IN p_id2 INT , IN limitStart INT, IN limitCount INT)
BEGIN
SET @SQL = CONCAT('
SELECT P.access_type INTO @privacy FROM Profile P WHERE P.profile_id = ' , p_id2 , ';
IF( ' , p_id1, ' = ' ,p_id2 , ')
THEN
SELECT T.task_id, T.name, D.add_time, D.location, DATE_FORMAT(D.date1, "%d/%m/%y") as `date1`, D.time3, D.state, TIME_FORMAT(D.time1, "%H:%i")as `time1`, D.does_id, IFNULL(L.Like, 0) AS `LikeCount` , IFNULL(C.CommentCount,0) AS `CommentCount` FROM Task T
INNER JOIN Does D on D.task_id = T.task_id
INNER JOIN Profile P on P.profile_id = D.profile_id
LEFT OUTER JOIN (
SELECT D.does_id, COUNT(L.profile_id) as `Like` FROM `Likes` L
INNER JOIN Does D on D.does_id = L.does_id
INNER JOIN Profile P on P.profile_id = D.profile_id
WHERE P.profile_id = ' , p_id2 , '
GROUP BY does_id) L on L.does_id = D.does_id
LEFT OUTER JOIN( SELECT D.does_id, COUNT(C.content) AS `CommentCount` FROM Comment C
INNER JOIN Does D on D.does_id = C.does_id
GROUP BY (D.does_id)) C ON C.does_id = D.does_id
WHERE P.profile_id= ' , p_id2, ' ORDER BY D.add_time DESC LIMIT ' , limitStart , ', ' , limitCount, ';
ELSE
IF (@privacy = 0)
THEN
SELECT T.task_id, T.name, D.add_time, D.location, DATE_FORMAT(D.date1, "%d/%m/%y") as `date1`, D.time3, D.state, TIME_FORMAT(D.time1, "%H:%i")as `time1`, D.does_id, IFNULL(L.Like,0) AS `LikeCount`, IFNULL(C.CommentCount,0) AS `CommentCount` FROM Task T
INNER JOIN Does D on D.task_id = T.task_id
INNER JOIN Profile P on P.profile_id = D.profile_id
LEFT OUTER JOIN (
SELECT D.does_id, COUNT(L.profile_id) as `Like` FROM `Likes` L
INNER JOIN Does D on D.does_id = L.does_id
INNER JOIN Profile P on P.profile_id = D.profile_id
WHERE P.profile_id = ' , p_id2 , '
GROUP BY does_id) L on L.does_id = D.does_id
LEFT OUTER JOIN( SELECT D.does_id, COUNT(C.content) AS `CommentCount` FROM Comment C
INNER JOIN Does D on D.does_id = C.does_id
GROUP BY (D.does_id) )C ON C.does_id = D.does_id
WHERE P.profile_id= ' ,p_id2, ' ORDER BY D.add_time DESC LIMIT ' , limitStart , ', ' , limitCount, ';
ELSE
IF EXISTS ( SELECT * FROM Follows F
INNER JOIN Profile P on F.follower_id = P.profile_id
INNER JOIN Profile P2 on F.following_id = P2.profile_id
WHERE (P.profile_id = ' , p_id1, ' AND P2.profile_id = ' , p_id2 , '))
THEN
SELECT T.task_id, T.name, D.add_time, D.location, DATE_FORMAT(D.date1, "%d/%m/%y") as `date1`, D.time3, D.state, TIME_FORMAT(D.time1, "%H:%i")as `time1`, D.does_id, IFNULL(L.Like,0) AS `LikeCount`, IFNULL(C.CommentCount,0) AS `CommentCount` FROM Task T
INNER JOIN Does D on D.task_id = T.task_id
INNER JOIN Profile P on P.profile_id = D.profile_id
LEFT OUTER JOIN (
SELECT D.does_id, COUNT(L.profile_id) as `Like` FROM `Likes` L
INNER JOIN Does D on D.does_id = L.does_id
INNER JOIN Profile P on P.profile_id = D.profile_id
WHERE P.profile_id = ' , p_id2 , '
GROUP BY does_id) L on L.does_id = D.does_id
LEFT OUTER JOIN( SELECT D.does_id, COUNT(C.content) AS `CommentCount` FROM Comment C
INNER JOIN Does D on D.does_id = C.does_id
GROUP BY (D.does_id) )C ON C.does_id = D.does_id
WHERE P.profile_id= ' , p_id2 , ' ORDER BY D.add_time DESC LIMIT ' , limitStart , ', ' , limitCount, ';
END IF;
END IF;
END IF; '
);
PREPARE query FROM @SQL;
EXECUTE query;
DEALLOCATE PREPARE query;
END
有人知道我们为什么会收到此错误吗?
ERROR : #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF( 18 = 18) THEN SELECT T.task_id, T.name, D.add_time, D.location, DATE_' at line 2
P.S : DELIMITER 设置为//
最佳答案
由于存储过程中查询的创建方式对服务器来说不是真正友好的(对其他开发人员也不友好),因此我重新格式化了存储过程。然后我还注意到它实际上是在三种情况下执行的相同查询,因此存在很多重复。
CREATE PROCEDURE getProfileTasks (IN p_id1 INT, IN p_id2 INT , IN limitStart INT, IN limitCount INT)
BEGIN
DECLARE privacy INT;
DECLARE is_following INT;
SELECT P.access_type INTO privacy FROM Profile P WHERE P.profile_id = p_id2;
SELECT COUNT(*) INTO is_following
FROM Follows F
INNER JOIN Profile P on F.follower_id = P.profile_id
INNER JOIN Profile P2 on F.following_id = P2.profile_id
WHERE (P.profile_id = p_id1 AND P2.profile_id = p_id2)
LIMIT 1;
IF ( (p_id1 = p_id2) OR (privacy = 0) OR (is_following > 0) )
THEN
-- Using CONCAT and prepared statement because LIMIT won't work otherwise
@SQL = CONCAT('
SELECT
T.task_id
, T.name
, D.add_time
, D.location
, DATE_FORMAT(D.date1, "%d/%m/%y") AS `date1`
, D.time3
, D.state
, TIME_FORMAT(D.time1, "%H:%i") AS `time1`
, D.does_id
, IFNULL(L.`Like`, 0) AS `LikeCount`
, IFNULL(C.CommentCount,0) AS `CommentCount`
FROM Task T
INNER JOIN Does D on D.task_id = T.task_id
INNER JOIN Profile P on P.profile_id = D.profile_id
LEFT OUTER JOIN (
SELECT D.does_id, COUNT(L.profile_id) AS `Like`
FROM `Likes` L
INNER JOIN Does D on D.does_id = L.does_id
INNER JOIN Profile P on P.profile_id = D.profile_id
WHERE P.profile_id = @p_id2
GROUP BY does_id
) L on L.does_id = D.does_id
LEFT OUTER JOIN (
SELECT D.does_id, COUNT(C.content) AS `CommentCount`
FROM Comment C
INNER JOIN Does D on D.does_id = C.does_id
GROUP BY (D.does_id)
) C ON C.does_id = D.does_id
WHERE P.profile_id= @p_id2
ORDER BY D.add_time DESC
LIMIT ', limitStart, ', ' , limitCount, ';');
SET @p_id2 = p_id2;
PREPARE query FROM @SQL;
EXECUTE query USING @p_id2;
DEALLOCATE PREPARE query;
END IF;
END
我还在 IFNULL(L.`Like`, 0) AS `LikeCount` 中的 Like 周围添加了反引号,因为这是问题的评论。
关于带有限制参数的 MySql 存储过程给出 err.no : 1064 when we use CONCAT statement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9454037/
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option
我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano
我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use
对于作为String#tr参数的单引号字符串文字中反斜杠的转义状态,我觉得有些神秘。你能解释一下下面三个例子之间的对比吗?我特别不明白第二个。为了避免复杂化,我在这里使用了'd',在双引号中转义时不会改变含义("\d"="d")。'\\'.tr('\\','x')#=>"x"'\\'.tr('\\d','x')#=>"\\"'\\'.tr('\\\d','x')#=>"x" 最佳答案 在tr中转义tr的第一个参数非常类似于正则表达式中的括号字符分组。您可以在表达式的开头使用^来否定匹配(替换任何不匹配的内容)并使用例如a-f来匹配一
我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些