json可以包含相同的键值吗?

时间:2016-03-21 17:14:33

标签: php mysql json pdo

我将两个表的内容执行这样的连接:

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"

如何看待我在两个表中有createdmodified相同的内容。因此,查询会丢弃created的{​​{1}}和modified ..这对我来说是一个问题,我该如何避免这种情况?

1 个答案:

答案 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",