比较不同表中数据略有不同的记录
Comparing records from different tables with slightly different data
我有两个 table。每个 table 都有产品信息和价格。我希望能够获得显示产品名称和两种价格的结果。
但是,每个 table 中的产品名称的写法略有不同。例如
Table 1
Name Price Pack Size
Aciclovir 200 mg Tablets 3.50 25
Aciclovir 400 mg Tablets 4.20 56
Aciclovir 800 mg Tablets 5.40 35
ACICLOVIR CREAM 2.40 GM
Table 2
ACICLOVIR 200MG TABs 1 25
ACICLOVIR 200MG TABs 1 25
ACICLOVIR 400MG TABs 2 56
Aciclovir 5% Cream 2gm 2.30 na
我试过 LIKE
,但我并没有真正得到我需要的结果。
Use trim where ever possible to remove spaces. This will help you
to make the column value similar.
For Example- 800 mg and 800MG .. by using trim you can get this same
value then use them in your query
在这种情况下(以及在 SQL 服务器中),我建议你使用这个:
REPLACE(a.Name, ' ', '') LIKE REPLACE(REPLACE(b.Name, ' ' ,''), 'TABs', 'TAB%s')
OR
REPLACE(b.Name, ' ' ,'') LIKE REPLACE(REPLACE(a.Name, ' ', ''), 'CREAM', '%CREAM%')
我可以简单地告诉你,当你有什么东西正在改变时,当你想忽略时,将它从你的比较中移除,' '
将它移除等等。当 MG
发生变化时,您也可以在比较中将其删除,我使用 REPLACE()
.
删除了此忽略文本
我有两个 table。每个 table 都有产品信息和价格。我希望能够获得显示产品名称和两种价格的结果。 但是,每个 table 中的产品名称的写法略有不同。例如
Table 1
Name Price Pack Size
Aciclovir 200 mg Tablets 3.50 25
Aciclovir 400 mg Tablets 4.20 56
Aciclovir 800 mg Tablets 5.40 35
ACICLOVIR CREAM 2.40 GM
Table 2
ACICLOVIR 200MG TABs 1 25
ACICLOVIR 200MG TABs 1 25
ACICLOVIR 400MG TABs 2 56
Aciclovir 5% Cream 2gm 2.30 na
我试过 LIKE
,但我并没有真正得到我需要的结果。
Use trim where ever possible to remove spaces. This will help you to make the column value similar.
For Example- 800 mg and 800MG .. by using trim you can get this same value then use them in your query
在这种情况下(以及在 SQL 服务器中),我建议你使用这个:
REPLACE(a.Name, ' ', '') LIKE REPLACE(REPLACE(b.Name, ' ' ,''), 'TABs', 'TAB%s')
OR
REPLACE(b.Name, ' ' ,'') LIKE REPLACE(REPLACE(a.Name, ' ', ''), 'CREAM', '%CREAM%')
我可以简单地告诉你,当你有什么东西正在改变时,当你想忽略时,将它从你的比较中移除,' '
将它移除等等。当 MG
发生变化时,您也可以在比较中将其删除,我使用 REPLACE()
.