Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql set variable using COUNT

I am trying to make a simple query to my server and want the result to be stored in the variable @times.

DECLARE @times int  SET @times = SELECT COUNT(DidWin)as "I Win" FROM thetable WHERE DidWin = 1 AND Playername='Me' 

IntelliSense says Wrong syntax near Select

like image 310
Lumpi Avatar asked May 22 '11 14:05

Lumpi


People also ask

How do you assign a COUNT to a variable in SQL?

You just need parentheses around your select: SET @times = (SELECT COUNT(DidWin) FROM ...) Or you can do it like this: SELECT @times = COUNT(DidWin) FROM ...

Can I use SELECT in COUNT?

SQL SELECT statement can be used along with COUNT(*) function to count and display the data values. The COUNT(*) function represents the count of all rows present in the table (including the NULL and NON-NULL values).


1 Answers

You just need parentheses around your select:

SET @times = (SELECT COUNT(DidWin) FROM ...) 

Or you can do it like this:

SELECT @times = COUNT(DidWin) FROM ... 
like image 186
Mike Valenty Avatar answered Sep 30 '22 08:09

Mike Valenty