简体   繁体   English

比较2个项目,但使用找到的项目中的值

[英]Comparing 2 items but using a value from found item

Having some issues trying to figure out how to write a SQL line to grab a value from an item. 尝试找出如何编写SQL行以从项目中获取值时遇到一些问题。 Let me explain the situation. 让我解释一下情况。 We have 2 different databases, one for domestic and one for import. 我们有2个不同的数据库,一个用于国内,一个用于进口。 They're the same items just different product codes. 它们是相同的物品,只是产品代码不同。 The difference is just the international items have a -h at the end. 不同之处在于国际项目的末尾带有-h。 So example.. 这样的例子

12345   < domestic 
12345-h < import

We have a feed going which combines both together but the import items are missing upc codes, while the domestic items have them. 我们有一个将两者结合在一起的提要,但是进口商品缺少upc代码,而国内商品却包含了这些代码。 So what I wanted to do was have it match both product codes and add the upc to the -h item. 所以我想做的是让它与两个产品代码匹配,然后将upc添加到-h项目中。

Any ideas? 有任何想法吗?

You can join both tables with 您可以使用

 INNER JOIN ON (concat(domestic.itemid, '-h') = international.itemid)

You might have to adjust this for the Ms Access SQL dialect. 您可能需要针对Ms Access SQL方言进行调整。 I think for concat you can use &, IIRC. 我认为对于concat,您可以使用&,IIRC。

I think you want a structure like this: 我认为您想要这样的结构:

select <fields that you want>
from ((select <list of fields from domestic>, itemid as lookupitem
       from domestic d
      ) union all
      (select <list of most fields from import>, replace(itemid, '-h', '') as lookupitem
       from import i
      )
     ) f left join
     lookup lu
     on f.lookupitemid = lu.itemid

In other words, you can create the field of the right form in a subquery and then join it to the lookup table used to get UPC. 换句话说,您可以在子查询中创建正确格式的字段,然后将其加入用于获取UPC的查找表。 I use "replace", on the assumption that some codes may not actually end with the "-h". 我使用“替换”,假设某些代码可能实际上未以“ -h”结尾。

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

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