简体   繁体   中英

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;

Then run your query : select count(id) into count_id from abc where ...

Then you can test that variable : 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. This will work in MySQL. Not sure about MS-SQL though.

select target_id, count(source_id) as num_links from mylinks group by target_id having num_links>5;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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