11

I'm querying a big mysql database with only read privileges, and I'd like to set some slow query results to a variable, 'foo', so I can use them again in other queries.

Basically, I want to have a variable for a cumbersome subquery, so I can reuse it without having the cost of running it every time I want to use it.

when I enter:

set @foo := (select *
            from table1 join table2 
            where bar = 0 
            group by id);

I get: ERROR 1241 (21000): Operand should contain 1 column(s) and if I restrict to 1 column, ERROR 1242 (21000): Subquery returns more than 1 row

Is there a way to store an array or a table in a variable? I don't have privileges to create temporary tables.

2 Answers 2

4

it should be @ when you are doing in MySQL.

set @foo := (select *
            from table1 join table2 
            where bar = 0 
            group by id);
5
  • Thanks, but now I get: ERROR 1241 (21000): Operand should contain 1 column(s). When I restrict it to 1 column, I get: Subquery returns more than 1 row.
    – 5un5
    Dec 6, 2012 at 5:04
  • yes because @foo can only store one value. so in your SELECT statement, you should definitely know that it will return single value.
    – John Woo
    Dec 6, 2012 at 5:05
  • Aah, I see. Thanks. Is there a way to store a table or at lease an array worth of query results in a variable?
    – 5un5
    Dec 6, 2012 at 5:09
  • hmmm, a subquery can atleast do :D
    – John Woo
    Dec 6, 2012 at 5:10
  • Sounding better and better, Thanks
    – 5un5
    Dec 6, 2012 at 7:12
2

You can also try this:

You cant store full table in any variable but you can store column data in any variable using below query.

SELECT GROUP_CONCAT(col1 SEPARATOR '~~~'), GROUP_CONCAT(col2 SEPARATOR '~~~'), ... INTO @foo, @foo2, ...
FROM table1 JOIN table2 
WHERE bar = 0 
GROUP BY id;

or

select col1, col2, ... into @foo, @foo2, ...
from table1 join table2 
where bar = 0 
group by id
9
  • Thanks-- way too much data to make this one work, but maybe for a smaller task.
    – 5un5
    Dec 6, 2012 at 5:11
  • It would be helpful, but I still get ERROR 1172 (42000): Result consisted of more than one row when I try it with:SELECT pid INTO @foo table1 join table 2 bar = 0 group by pid
    – 5un5
    Dec 6, 2012 at 5:23
  • Try group_concat function to get all rows data in one variable as I have already defined in my answer. Dec 6, 2012 at 5:35
  • From: SELECT GROUP_CONCAT(pid SEPARATOR '~~~') INTO @foo from table1 join table2 where bar=0; I got, ERROR 1172 (42000): Result consisted of more than one row. With 2 columns, I got the same error.
    – 5un5
    Dec 6, 2012 at 5:45
  • Please provide the output of this query (SELECT id, pid FROM table1 JOIN table2 WHERE bar = 0 GROUP BY id) and also provide the what the output you exactly want. Dec 6, 2012 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.