5

我有像这样的桌子

changes201101
changes201102
changes201103
...
changes201201

whichchanges以及包含行 Year 和 MONTH 的表

如何从更改中选择 * whichchanges

我输入这个查询

SET @b := SELECT CONCAT('changes',year,month) FROM whichchanges;
((((@b should contain now multiple rows of changesYearMonth)))))
SET @x := SELECT * FROM @b;
Prepare stmt FROM @b;
Prepare stmt FROM @x;
Execute stmt;

#1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'SELECT CONCAT('changes',year,month) FROM changes)' 附近使用正确的语法

4

1 回答 1

7

您打开 1(并关闭 2 )。删除最后一个:

SELECT CONCAT('changes',year,month) FROM changes

编辑

第二个陈述应该是

SET @x := SELECT * FROM (@b) as b;

这有效,但不确定这是否是您想要的:

SET @b := 'SELECT CONCAT(''changes'',`year`,`month`) FROM whichchanges';
SET @x := 'SELECT * FROM (SELECT CONCAT(''changes'',`year`,`month`) FROM whichchanges) as b';
Prepare stmt FROM @b;
Prepare stmt FROM @x;
Execute stmt;

编辑2

如果我理解正确,您正在寻找那个单一的查询:

select * from changes
where change_column in (select distinct concat(`year`, `month`) from whichchanges)

编辑3

select @b := group_concat(concat(' select * from changes', `year`, `month`, ' union ') separator ' ') as w from whichchanges;
set @b := left(@b, length(@b) - 6);

Prepare stmt FROM @b;
Execute stmt;

SQLFiddle 示例

于 2012-05-07T10:26:24.037 回答