简体   繁体   English

如何将值从一列连接到另一列

[英]How to join values from one column to another

I have two tables Notification and Worker Notifications. 我有两个表Notification和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. 我的工作人员类继承了Notification,我想将所有先前存在的工作时间与通知时间一起加入。

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 从items_a中选择category_id,col1,col2,col3从项目_b中选择category_id,col1,col2,col3

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 我之所以这样说,是因为通知中有一个workerid,所以它显然“属于某个工作人员”-不一定基于时间与该工作人员相关,就好像两次相同,那么您最终可能会加入分配给约翰,戴夫·英特

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). JOIN使您的结果集横向增长(更多列)。 UNION causes your result set to grow vertically (more rows) UNION导致结果集垂直增长(更多行)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从一个列中加入多个值,从另一个表中选择 - Join multiple values from one column, selected from another table 如何将一个表中的两个或更多列与另一个表中的一个列联接在一起,即使第一个表列中的值为NULL - How to join two or more columns from one table with one column from another table, even there are NULL values in first table columns 如何使用联接将列从一个表复制到另一个表 - How to copy column from one table to another using JOIN 如何将一列连接到另一个表中的所有列 - how to join one column to all columns from another table 如何用一个来自另一个表的列的值约束一个列? - How to constraint one column with values from a column from another table? 如何使用连接将值从一个表链接到另一个表 - How to use join to link values from one table to another 如何从联接查询中获取一列的DISINCT值 - How to get DISINCT values of one column from a join query 将一个表中的列值连接到另一个表的新列中的行 - Join column values from one table to rows in new column on another table 联接中另一个表的不同列值 - DISTINCT COLUMN VALUES FROM ANOTHER TABLE IN JOIN 如何在保留另一列中的值的同时连接一列上的两个表? - How do I join two tables on one column while preserving the values in another column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM