SQL如何根据联接表对所有结果进行分组

时间:2020-09-25 18:16:39

标签: mysql sql sql-server database

我需要记录交易,市场交易和车站交易的“帮助加入”表。 我想根据位置获取结果,车站交易表从联接表获取其位置。

表如下:

market_transations
+------------------------------------------------------------------+---------------------+
| m_trans_id  | markeeter_id  | amount  | receipt  |   loction_id  |     trans_date      |
+-------------+---------------+---------+----------+---------------+---------------------+
|    1        +     1         |  20     |  abc123  |     1         | 2020-09-16 23:20:00 |
+-------------+---------------+---------+----------+---------------+---------------------+
|    2        +     2         |  20     |  xcv897  |     1         | 2020-09-18 09:00:00 |
+-------------+---------------+---------+----------+---------------+---------------------+
|    3        +     6         |  10     |  qwe563  |     3         | 2020-09-19 12:00:00 |
+-------------+---------------+---------+----------+---------------+---------------------+

station_transactions
+------------------------------------------------------------------+---------------------+
| s_trans_id  |   draver_id   | amount  | receipt  |   station_id  |     trans_date      |
+-------------+---------------+---------+----------+---------------+---------------------+
|    1        +     1         |  5      |  123456  |     1         | 2020-09-16 13:20:00 |
+-------------+---------------+---------+----------+---------------+---------------------+
|    2        +     2         |  50     |  qwerty  |     2         | 2020-09-17 09:00:00 |
+-------------+---------------+---------+----------+---------------+---------------------+
|    3        +     6         |  10     |  zxcvbn  |     3         | 2020-09-18 11:00:00 |
+-------------+---------------+---------+----------+---------------+---------------------+

locations
----------+-----------+
| id      | name      |
+---------+-----------+
|  1      |   a       |
+---------+-----------+
|  2      |   b       |
+---------+-----------+
|  3      |   c       |
+---------+-----------+
|  4      |   d       |
+---------+-----------+

stations
---------+---------+-------------+
|   id   |  name   | loctaion_id |
+--------+---------+-------------+
|   1    |  st1    |   1         |
+--------+---------+-------------+
|   1    |  st2    |   2         |
+--------+---------+-------------+
|   1    |  st3    |   3         |
+--------+---------+-------------+
 

预期结果

-------------------+-----------+---------------+---------+----------+-----------------------+
|   location_name  | driver_id | markeeter_id  | amount  | receipt  |      trans_date       |
+------------------+-----------+---------------+---------+----------+-----------------------+
|      a           |     1     |     NULL      |   5     |  123456  |   2020-09-16 13:20:00 |
+------------------+-----------+---------------+---------+----------+-----------------------+
|      a           |     NULL  |     1         |   20    |  abc123  |   2020-09-16 23:20:00 |
+------------------+-----------+---------------+---------+----------+-----------------------+
|      b           |     2     |     NULL      |   50    |  qwerty  |   2020-09-17 09:00:00 |
+------------------+-----------+---------------+---------+----------+-----------------------+
|      c           |     6     |     NULL      |   10    |  zxcvbn  |   2020-09-18 11:00:00 |
+------------------+-----------+---------------+---------+----------+-----------------------+
|      a           |     NULL  |     2         |   20    |  xcv897  |   2020-09-18 09:00:00 |
+------------------+-----------+---------------+---------+----------+-----------------------+
|      c           |     NULL  |     3         |   10    |  qwe563  |   2020-09-19 12:00:00 |
+------------------+-----------+---------------+---------+----------+-----------------------+
      

2 个答案:

答案 0 :(得分:1)

虽然我无法测试查询,但这应该工作



SELECT  l.name AS location_name, NULL as driver_id, mt.markeeter_id, mt.amount, mt.receipt, mt.trans_date  
  FROM  market_transations mt, locations l
 WHERE  l.id = mt.location_id

UNION

SELECT  l.name AS location_name, st.driver_id, NULL AS markeeter_id, st.amount, st.receipt, st.trans_date  
  FROM  station_transactions st, stations s, locations l
 WHERE  st.station_id = s.id AND s.location_id l.id

答案 1 :(得分:1)

SELECT locations.name as name ,driver_id,NULL as markeeter_id,st.amount as amount, st.receipt as receipt, st.trans_date as trans_date FROM locations 
JOIN stations ON  locations.id = station.location_id
JOIN station_transactions st ON station.id = st.station_id  
order by trans_date ASC  

UNION

SELECT locations.name as name ,NULL as driver_id,markeeter_id,mt.amount as amount, mt.receipt as receipt, mt.trans_date as trans_date FROM locations
JOIN market_tramlsactop mt ON locations.id = mt.location_id 
order by trans_date ASC