tags:

views:

106

answers:

1

I have an issue regarding an recursive count which works well in SQL server, but I can not make this work in mySQL.

In SQL Server I used to use:

SELECT
@Param= not_unique_id,
(Select Count(not unique id) FROM TABLE0 WHERE not_unique_id=@Param)
FROM Table0 WHERE .....

this will give me:
1 2
1 2
2 1
3 3
3 3
3 3

this will give me the number of id's per current row id as parameter

In mySQL i do the same:

SELECT
Param=not_unique_id,

(returns NULL, SET is not allowed here)

(Select Count(not unique id) FROM TABLE0 WHERE not_unique_id=Param)
FROM Table0 WHERE .....

this will give me:
NULL NULL
NULL NULL
NULL NULL
NULL NULL
NULL NULL
NULL NULL

Alternatively, I tried with: In mySQL i do the same:

SELECT
not_unique_id,
(Select Count(not unique id) FROM TABLE0 WHERE not_unique_id=not_unique_id)
FROM Table0 WHERE .....

this will give me:
1 3
1 3
2 3
3 3
3 3
3 3

this will give me the unique count of the whole record

any idea how to do this in mySQL?

+2  A: 

For assigning a value in mysql don't forget :=

SELECT @Param:=not_unique_id,
(
 SELECT Count(not_unique_id)
 FROM TABLE0
 WHERE not_unique_id=@Param
)
FROM Table0 WHERE .....

or

SELECT not_unique_id,
(
 SELECT Count(not_unique_id)
 FROM TABLE0
 WHERE not_unique_id=a.not_unique_id
)
FROM Table0 a WHERE .....
Patrick
First one didn't work, but the second one worked.
solkim
HI Patrick, can you name what kind of query you have written to get the results, is it any sort of subquery. Having select inside one more select. Just want to know the terminology for this kind of query
harigm
@solkim Do you have an error on the first one ?
Patrick
@harigm I have no special name for that query
Patrick
@Patrick Yes, there was an error. Couldn't assign the row value to the parameter.
solkim
@harigm It is a subselect, that is dependent of current row values as a paremeter.
solkim