我正在尝试将数据库中两个不同表的两列合并为一列。
cust_tbl(表 1):
JL_JOB_NO |
-----------
1 |
2 |
项目(表 2):
prjID |
-----------
3 |
4 |
我希望我的输出是:
new |
-----------
1 |
2 |
3 |
4 |
我尝试过:
SELECT 'JL_JOB_NO' AS 'new'
FROM 'cust_tbl'
UNION
SELECT 'prjID' as 'new'
FROM 'projects';
最佳答案
使用反引号 (`) 代替单引号(或直接删除它们):
试试这个:
SELECT `JL_JOB_NO` AS `new`
FROM `cust_tbl`
UNION
SELECT `prjID` as `new`
FROM `projects`;
或者只是:
SELECT JL_JOB_NO AS new
FROM cust_tbl
UNION
SELECT prjID as new
FROM projects;
Side note: Back ticks are to be used for table and column identifiers, but are only necessary when the identifier is a MySQL reserved keyword, or when the identifier contains whitespace characters or characters beyond a limited set it is often recommended to avoid using reserved keywords as column or table identifiers when possible, avoiding the quoting issue.
在以下情况下需要反勾号:
SELECT id, `my name`, `another field` , `field,with,comma`
关于MySQL 将多个表中的行组合成一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23412538/