简体   繁体   中英

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.

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.

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.

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