简体   繁体   中英

How to join values from one column to another

I have two tables Notification and Worker Notifications. On those tables there are two similar columns called Time. My worker class inherits Notification and I would like to join all preexisting times on worker with notification time.

id | Worker| Time
-------------
10 | John| 8/17/2019
20 | Rui| 8/17/2019
30 | Pen| 8/17/2019
id | Notification| Time |WorkerID
-----------------------------------
10 | John| 8/17/2019 | 10
20 | Rui| 8/17/2019  |20
30 | Pen| 8/17/2019  |30

SELECT category_id, col1, col2, col3 FROM items_a UNION SELECT category_id, col1, col2, col3 FROM items_b

I dare say it should actually be

SELECT *
FROM 
  worker w 
  INNER JOIN
  notifications n
  ON 
    w.id = n.workerid

I say this because a notification has a workerid so it clearly "belongs to a worker" - it isn't necessarily related to a worker based on the time, as if two times are the same then you could end up joining a notification assigned to John, to Dave intead

But if you insist on joining on the time the pattern is the same:

SELECT *
FROM 
  worker w 
  INNER JOIN
  notifications n
  ON 
    w.time = n.time 

JOIN causes your result set to grow sideways (more columns). UNION causes your result set to grow vertically (more rows)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM