jeudi 18 décembre 2014

storing query results in a variable in mysql and using it in another query


I have a following query which runs perfectly fine:



SELECT count(*) as total,l.state as state
FROM
db1.table1 cd
inner join db2.table2 l ON
LEFT(cd.threestring_vch, 3) = l.NPA
AND
MID(cd.threestring_vch, 4,3) = l.NXX
WHERE
starttime_dt >= '2014-12-18 00:00:00'
AND
starttime_dt <= '2014-12-18 23:59:59'
AND
Result_int = 76
GROUP BY
l.state


However, when I try to do the following and run the query, I get and error:



Error Code: 1241. Operand should contain 1 column(s) 0.016 sec


Not sure, what is wrong here as I am referring to one related post which says to do what I am doing right now.



SET @v1 := (SELECT count(*) as total,l.state as state
FROM
db1.table1 cd
inner join db2.table2 l ON
LEFT(cd.threestring_vch, 3) = l.NPA
AND
MID(cd.threestring_vch, 4,3) = l.NXX
WHERE
starttime_dt >= '2014-12-18 00:00:00'
AND
starttime_dt <= '2014-12-18 23:59:59'
AND
Result_int = 76
GROUP BY
l.state);


Here is my ultimate goal:


I want to use the value returned by above query in above variable v1 like the following:



SELECT state , SUM(total) as TOTAL

FROM v1
GROUP BY state
ORDER BY state ASC


So basically I will be doing UNION of above two queries.


Please let me know what I am doing wrong.


Thanks





Aucun commentaire:

Enregistrer un commentaire