在MySQL中使用JOINS,会创建一个附加表吗?

时间:2016-12-19 02:47:17

标签: mysql sql outer-join

SELECT d.user_guid AS dUserID, c.user_guid AS cUserID, d.dog_guid AS dDogID,
c.dog_guid AS dDogID, count(test_name)
FROM dogs d LEFT JOIN complete_tests c
ON d.dog_guid=c.dog_guid
GROUP BY d.dog_guid;

编写此代码时,会创建一个标记为..." dDOGId_1"

输出实际上是相同的,除了一些有"非"在行

这是JOIN发生的地方吗?这也是从哪里来的?我的代码中是否存在创建此附加列的错误?

2 个答案:

答案 0 :(得分:3)

什么?你似乎很困惑。这是您的select

SELECT d.user_guid AS dUserID, c.user_guid AS cUserID, d.dog_guid AS dDogID,
       c.dog_guid AS dDogID, count(test_name)

未创建添加。您的结果有两列名为dDogID。为了区分它们,应用程序正在添加_1

然而,包括两者完全没必要,因为left join意味着它们是相同的。并且count()应该计算匹配。我假设这是你想要的查询:

SELECT d.user_guid AS dUserID, d.dog_guid AS dDogID,
       count(c.test_name)
FROM dogs d LEFT JOIN
     complete_tests c
     ON d.dog_guid=c.dog_guid
GROUP BY d.dog_guid;

c.user_guid SELECTMIN(c.user_guid)中没有意义,因为不清楚它应该来自哪一行。您可能需要MAX()GROUP_CONCAT()mail

答案 1 :(得分:0)

这种情况正在发生,因为您有2个标有相同名称的列。所以第二个默认为_1

它与联接

无关