简体   繁体   English

合并具有一列部分共有的两个表

[英]Merge two tables having one column partially in common

I have two table and, joining them, I obtain some particular records but I would like to add one column to the obtained query;我有两个表,加入它们后,我获得了一些特定的记录,但我想在获得的查询中添加一列; these are the tables in question这些是有问题的表格

tables

This is the code that I use这是我使用的代码

select a.uniqueid,null as uni,b.freetxt,b.free
from (select distinct uniqueid from uniqueid_5) a
inner join freetxt_5 b
on a.uniqueid=b.freetxt or b.freetxt like a.uniqueid+'[ -]%'

Now I would like to add one column (uni)to the table obtained, adding the records that are still in the table uniqueid_5 but with join I add more records than what I need.现在我想在获得的表中添加一列(uni),添加仍然在表 uniqueid_5 中的记录,但是通过连接我添加的记录比我需要的要多。

I assume your query returned more rows than expected.我假设您的查询返回的行数比预期的多。
In this particular case, returning the uni column will "invalidate" the distinct or better said it won't work the way you wanted it to as it will now return only UNIQUE PAIRS OF UNIQUEID AND UNI which in return will get you more rows returned.在这种特殊情况下,返回uni列将使distinct的列“无效”,或者更好地说它不会按照您想要的方式工作,因为它现在只会返回UNIQUE PAIRS OF UNIQUEID 和 UNI ,这反过来会让您返回更多行.

For every uniqueid 'a' in unique_5 the query will return all rows from freetxt_5 that contain 'a' or 'a - 1' in the freetxt column.对于uniqueid中的每个唯一标识“a”,查询将返回freetxt_5中在unique_5列中包含“a”或“a - 1”的freetxt行。

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

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