mysql - 如何使用 select 语句的结果作为列名?
问题描述
我想将 select 语句的结果用作另一个 select 语句中的列名。我还想删除所有内容,包括表名的最后一个“_”,以便我可以在 where 子句中使用剩余的字符。
select
voters.name,
id,
vote
from tbl1_compiled_number
inner join voters
on tbl1_compiled_number.id = voters.id;
按预期工作。
我现在想要的是将投票列标记为以下结果:
select
question
from votes
where concat(left(loc,1),id)=number // 1st character of the loc column and the id column equals "number". This is the "number" portion from table_compiled_number table name.
结果是“鸡可以过马路”
我最终想要的是:
select
voter.name,
id,
vote as (select questions from voters where concat(left(loc,1),id) = number)
from tbl1_compiled_number
inner join voter
on tbl1_compiled_number.id = voter.id;
结果应如下所示:
+------------+--------+-----------------------------------+
| name | id | Can chickens cross the road alone |
+------------+--------+-----------------------------------+
| Bugs Bunny | ABC123 | No |
| Daffy Duck | DEF456 | No |
| Spiderman | GHI789 | Yes |
+------------+--------+-----------------------------------+
解决方案
推荐阅读
- sql - 无法从 SQL Server 中的表中删除“第一条”记录
- java - JUnit 用于测试单链表的反转
- javascript - 未从 AJAX '$_POST' 到 PHP 文件执行 SQL 查询
- python - 生成真/假的所有长度-n 排列?
- c# - 来自客户端html和服务器端C#的POST方法有什么区别
- multithreading - 多线程行为从机器更改为其他
- php - 为 Woocommerce 在 quantity-input.php 中添加数量单位
- angular - 角度:如何解决这个问题
- javascript - Firebase 云存储/功能 - 错误:上传图片时超出配额
- java - 如何使用休眠填充 JComboBox?