本文介绍了如何从动态表名中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有表如
changes201101
changes201102
changes201103
...
changes201201
和表
,其中包含行年和月份
And tablewhichchanges which contain rows Year and MONTH
我可以从哪些更改中选择*?
How i can select * from changes from 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 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT CONCAT('changes',year,month) FROM changes)' at line 1
推荐答案
您打开1 (
并关闭2 )
。删除最后一个:
You open 1 (
and close 2 )
. Remove the last:
SELECT CONCAT('changes',year,month) FROM changes
编辑
第二个语句应该是
Edit
the second statement should probably be
SET @x := SELECT * FROM (@b) as b;
这是有效的,但不知道如果这是你想要的:
That works, but not sure if that is what you want:
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;
Edit2
如果我明白你的权利您正在寻找单一查询:
Edit2
If I understood you right you are looking for that single query:
select * from changes
where change_column in (select distinct concat(`year`, `month`) from whichchanges)
Edit3
Edit3
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;
这篇关于如何从动态表名中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!