在MySQL查询中使用列名作为表

在MySQL查询中使用列名作为表,mysql,Mysql,我有一个数据库,其中一个表中的列是我需要在其中查找相应记录的表名 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | TABLE questions

我有一个数据库,其中一个表中的列是我需要在其中查找相应记录的表名

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| TABLE questions                                                                                                                                                                                 |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Field                   | Type                                                                                                                                                                  |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| question_id             | int(11)                                                                                                                                                               |
| question_response_table | enum('question_responses_datetime','question_responses_int','question_responses_float','question_responses_bool','question_responses_text','question_responses_enum') |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
question\u response\u table
列中的每个值都是另一个保存用户响应的表(原始数据库设计要求将响应按数据类型分离到单独的表中)

我有一个查询,除了用户的响应之外,它可以得到我所需要的一切。但出于性能原因,我真的很想将该响应附加到同一个查询上(数据库每年都会显著增长)。下面是我想做的一个例子:

SELECT cr.category_id, cr.category_response_id, r.response 
FROM category_responses AS cr 
JOIN response_key_questions AS rkq ON cr.category_id = rkq.category_id 
JOIN questions AS q ON q.question_id = rkq.question_id
JOIN {q.question_response_table} AS r ON r.category_response_id = cr.category_response_id 
WHERE cr.belongs_to = 4 AND cr.reporting_year_id = 1 AND cr.date_retired IS NULL 
ORDER BY cr.category_id

这会变得很难看。最好创建一个视图,将
response
列添加到
cr
表中

SELECT cr.category_id, cr.category_response_id,
    CASE 
    WHEN q.question_response_table = '...' THEN
        (SELECT response
         FROM ... r
         WHERE r.category_response_id = cr.category_response_id)
    ... 
    ELSE '' 
    END AS response
FROM category_responses AS cr 
JOIN response_key_questions AS rkq ON cr.category_id = rkq.category_id 
JOIN questions AS q ON q.question_id = rkq.question_id
WHERE cr.belongs_to = 4 AND cr.reporting_year_id = 1 AND cr.date_retired IS NULL 
ORDER BY cr.category_id

您需要类似于
eval()
的东西,这在SQL中是不存在的,而且理由非常充分;模式的要点在于它严格定义了不同数据段之间可能存在的关系,并且允许使用任意字符串作为列或表名,这使得这是一项不可能完成的任务。您最好在一个表中使用响应进行重构,并用外键定义关系,但如果您不能这样做,那么您可能会发现一些有用的东西。@eggyal不幸的是,我没有选择DB的设计方式,事实就是这样,因此有数十万条记录far@AaronMiller我尝试过类似的方法,MySQL抱怨variableNot中有不止一个值,这是最干净的方法,但在我的情况下它是有效的。非常感谢!