我将两个表的内容执行这样的连接:
SELECT * FROM table1
INNER JOIN table2
ON table1.code = table2.code
现在table1
的结构如下:
|CODE|Info|Created |Modified
|R789|Home|21/03/2016 10:00 |21/03/2016 15:00
和table2
:
|CODE|Description|Created |Modified
|R789|Testing| 21/03/2016 10:05 | 21/03/2016 18:10
现在的问题是查询返回了这个结果:
"Code":"RB01",
"Info":Home,
"Created":"21/03/2016 10:05",
"Modified":"21/03/2016 18:10",
"Description":"Testing"
如何看待我在两个表中有created
和modified
相同的内容。因此,查询会丢弃created
的{{1}}和modified
..这对我来说是一个问题,我该如何避免这种情况?
答案 0 :(得分:4)
您需要使用AS keyword to create an alias作为列名。考虑一下:
SELECT t1.CODE, t1.Info, t1.Created AS t1Created, t1.Modified AS t1Modified, t2.Description, t2.Created AS t2Created, t2.Modified AS t2Modified
FROM table1 t1
INNER JOIN table2 t2 ON t1.CODE = t2.CODE
这将返回
"Code":"RB01",
"Info":Home,
"t1Created":"21/03/2016 10:00",
"t1Modified":"21/03/2016 15:00",
"Description":"Testing",
"t2Created":"21/03/2016 10:05",
"t2Modified":"21/03/2016 18:10",