简体   繁体   English

MySQL的存储过程,如果(选择...)>变量然后

[英]Mysql stored procedure if (select…) > variable then

I want to do something like: 我想做类似的事情:

if ((select count(id) from abc where ...) > variableINT) then .....

Why does this doesn´t work properly? 为什么这不能正常工作?

Do the following in your stored procedure (I assume you basic knowledge of declaring variables and writing queries) : 在存储过程中执行以下操作(我假设您具有声明变量和编写查询的基本知识):

Declare a variable count_id : count_id NUMBER; 声明一个变量count_id: count_id NUMBER;

Then run your query : select count(id) into count_id from abc where ... 然后运行您的查询: select count(id) into count_id from abc where ...

Then you can test that variable : if (count_id > ...) then .... end if 然后可以测试该变量: if (count_id > ...) then .... end if

Let's say you have a link table (mylinks) with links from source_id to target_id and you want to find only the targets that have more than 5 links. 假设您有一个链接表(mylinks),其中包含从source_id到target_id的链接,而您只想查找具有5个以上链接的目标。 This will work in MySQL. 这将在MySQL中工作。 Not sure about MS-SQL though. 虽然不确定MS-SQL。

select target_id, count(source_id) as num_links from mylinks group by target_id having num_links>5; 从mylinks组选择num_links> 5的target_id,count(source_id)作为num_links;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM