如何从两组表格中区分出来

时间:2018-12-02 12:42:02

标签: mysql

我有两套桌子,一套叫做成功,另一套是不成功的,我做了一个联合,我希望能够对球员的名字进行不同的计数。两组都具有相同的列名。

SELECT
  s.date,
  s.location,
  sn.name
FROM
  s_location s
  INNER JOIN s_players sp ON sp.p_id = s.id
  INNER JOIN s_name sn ON sn.id = sp.p_id
WHERE
  s.date BETWEEN '2018-11-16' AND '2018-11-16'
UNION
SELECT
  u.date,
  u.location,
  un.name
FROM
  u_location u
  INNER JOIN u_players up ON up.p_id = u.id
  INNER JOIN u_name un ON un.id = up.p_id
WHERE
    u.date BETWEEN '2018-11-16' AND '2018-11-16'
ORDER BY
  location

成功的表 位置表s_location

+-----------+----------+------------+
| id        | location | date       |
+-----------+----------+------------+
| 10        | LOCA     | 2018-11-18 |
+-----------+----------+------------+

玩家表s_players

+-----------+----------+
| LOC_id    | p_id     |
+-----------+----------+
| 10        | 667      |
| 10        | 104      |
| 10        | 733      | 
+-----------+----------+

玩家姓名表s_name

+-----------+----------+
| id        | name     |
+-----------+----------+
| 667       | John     |
| 104       | Peter    |
| 733       | Mary     |
+-----------+----------+

不成功的表 位置表u_location

+-----------+----------+------------+
| id        | location | date       |
+-----------+----------+------------+
| 11        | LOCB     | 2018-11-18 |
+-----------+----------+------------+

玩家表u_players

+-----------+----------+
| LOC_id    | p_id     |
+-----------+----------+
| 11        | 667      |
| 11        | 114      |
| 11        | 243      | 
+-----------+----------+

玩家姓名表u_name

+-----------+----------+
| id        | name     |
+-----------+----------+
| 667       | John     |
| 114       | Sally    |
| 733       | James    |
+-----------+----------+

两组结果表

+------------+----------+------------+
| date       | location | name       |
+------------+----------+------------+
| 2018-11-18 | LOCA     | John       |
| 2018-11-18 | LOCA     | Peter      |
| 2018-11-18 | LOCA     | Mary       |
| 2018-11-18 | LOCB     | John       |
| 2018-11-18 | LOCB     | Sally      |
| 2018-11-18 | LOCB     | James      |
+------------+----------+------------+

我该如何对名称进行不同的计数,以便我得到5(两个表(成功与失败)中都为John(p_id 667))

我追求的结果是

+------------+------------+
| date       | name       |
+------------+------------+
| 2018-11-18 | John       |
| 2018-11-18 | Peter      |
| 2018-11-18 | Mary       |
| 2018-11-18 | Sally      |
| 2018-11-18 | James      |
+------------+------------+

1 个答案:

答案 0 :(得分:0)

将以上查询与另外一个在namedate列上具有唯一性的选择进行包装。

SELECT DISTINCT t.date, t.name
FROM (
    SELECT
      s.date,
      s.location,
      sn.name AS name
    FROM
      s_location s
      INNER JOIN s_players sp ON sp.p_id = s.id
      INNER JOIN s_name sn ON sn.id = sp.p_id
    WHERE
      s.date BETWEEN '2018-11-16' AND '2018-11-16'
    UNION
    SELECT
      u.date,
      u.location,
      un.name AS name
    FROM
      u_location u
      INNER JOIN u_players up ON up.p_id = u.id
      INNER JOIN u_name un ON un.id = up.p_id
    WHERE
        u.date BETWEEN '2018-11-16' AND '2018-11-16'
    ORDER BY
      location
) t