[主要编辑以使事情清楚]
我想编写一个返回动态列名的查询,如下所示:
SELECT
f2 AS
(
SELECT column_name
FROM column_names_tbl
WHERE column_name = "experience"
limit 0,1
)
FROM some_table
所以输出与此相同:
SELECT
f2 AS experience
FROM some_table
答案 0 :(得分:0)
这不是正确的SQL语法,即使两个查询(所选字段及其别名)都是子查询并且彼此无关。所以,即使语法正确,mysql也不可能区分你要连接的名称,即使语法是正确的......
您已经使用了或多或少规范化的关系表,因此我建议采用以下解决方案:
SELECT ps.keyname, psv.keyvalue FROM page_setting_values AS psv INNER JOIN page_settings AS ps ON ps.id = psv.setting_id WHERE psv.page_revision_id = :revision
with:revision表示您的修订版ID 您现在可以从该结果集中组装关联的数组:
$settings = [];
$result = $db->executeQuery('...')->fetchAll();
foreach($result as $setting)
{
$settings[$setting['keyname']] = $setting['keyvalue'];
}
希望有所帮助;)