4

このようなテーブルが2つあり、

表1

Id     Locations
--     ---------
1      India, Australia
2      US , UK 

表2

Table2Id    Location
--------    --------
101         Italy
102         UK
103         Hungary
104         India

テーブル2にテーブル1のフィールドがLocations含まれて いる場合、これら2つのテーブルを条件で内部結合する必要があります。Location結果は次のようになります

Id   Table2Id    Location     Locations
--   --------    --------     ---------
1     104        India        India, Australia
2     102        UK           US , UK 

私は何かを試しました

Select t1.id,
       t2.Table2Id,
       t1.Locations,
       t2.Location
From Table1 t1 
Inner join Table2 t2 On CONTAINS(t1.Locations, t2.Location)

ただし、 の 2 番目のパラメーターはcontains文字列にする必要があります。そこに列名を付けることはできません。

クエリでtemptableorを使用できません。このクエリは、とがサポートされていないvariableメール キャンペーン ツールで実行する必要があるためです。ExactTargettemptablevariables

どんな助けでも大歓迎です。ありがとうございました。

4

3 に答える 3

11

MySQL 5.5 の SQLFiddle の例SQLのSQLFiddle の例

テーブルとデータ

create table table1 (id int, locations varchar(100));
insert into table1 values 
(1, 'India, Australia'),
(2, 'US, UK');

create table table2 (table2id int, location varchar(100));
insert into table2 values
(101, 'Italy'),
(102, 'UK'),
(103, 'Hungary'),
(104, 'India');

MySQL クエリ

select
  table1.id,
  table2.table2id,
  table2.location,
  table1.locations
from table1
join table2 on table1.locations like concat('%', table2.location, '%')

SQL Server クエリ

select
  table1.id,
  table2.table2id,
  table2.location,
  table1.locations
from table1
join table2 on table1.locations like '%' + table2.location + '%'

編集

米国の場所が国名オーストラリアに含まれている場合、上記のクエリは期待どおりに機能しない場合があります。この問題を回避するには、次のクエリを使用できます

select
  table1.id,
  table2.table2id,
  table2.location,
  table1.locations
from table1
join table2 on 
  ',' + replace(table1.locations,', ', ',') + ',' like '%,' + table2.location + ',%'

このクエリは強制的India, Australiaに になり,India,Australia,ます。これは次に比較される,US,ため、不正確な結果に悩まされることはありません。

于 2014-09-23T05:31:49.083 に答える
1

Mysqlを使用している場合は、次のオプションを確認できます。 INSTR

select table2id, location, table1.id, locations
from table2 inner join table1 on instr(locations,location) >= 1; 

SQL フィドル リンク

于 2014-09-23T07:01:01.930 に答える