[英]MySQL combine rows from multiple tables into on row
I'm trying to combine two columns from two different tables in my database into one. 我正在尝试将数据库中两个不同表中的两列组合成一列。
cust_tbl (table 1): cust_tbl(表1):
JL_JOB_NO |
-----------
1 |
2 |
projects (table 2) : 项目(表2) :
prjID |
-----------
3 |
4 |
I'd like my output to be: 我希望我的输出是:
new |
-----------
1 |
2 |
3 |
4 |
I tried: 我试过了:
SELECT 'JL_JOB_NO' AS 'new'
FROM 'cust_tbl'
UNION
SELECT 'prjID' as 'new'
FROM 'projects';
Use backticks (`) instead of single quotes (or just remove them): 使用反引号(`)而不是单引号(或只删除它们):
Try this: 尝试这个:
SELECT `JL_JOB_NO` AS `new`
FROM `cust_tbl`
UNION
SELECT `prjID` as `new`
FROM `projects`;
Or just: 要不就:
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.
附注:后面的刻度将用于表和列标识符,但仅当标识符是MySQL保留关键字时才需要,或者当标识符包含空格字符或超出有限集的字符时,通常建议避免使用保留关键字尽可能作为列或表标识符,避免引用问题。
Back ticks are necessary for situations like the following: 对于以下情况,后退滴答是必要的:
SELECT id, `my name`, `another field` , `field,with,comma`
而不是'(单引号)使用`(反引号)或删除'(单引号)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.