1

次の表を検討してください。

 drop table if exists testA;
 drop table if exists testB;
 create table testA ( id int, testX varchar(255) );
 create table testB ( id int, testY varchar(255) );
 insert into testA values ( 1, 'this is a test%' );
 insert into testB values ( 1, 'test%' );
 insert into testA values ( 1, 'a test% this is' );

この結果:

mysql>  select * from testA;
+------+-----------------+
| id   | testX           |
+------+-----------------+
|    1 | this is a test% |
|    1 | a test% this is |
+------+-----------------+
2 rows in set (0.04 sec)

mysql>  select * from testB;
+------+-------+
| id   | testY |
+------+-------+
|    1 | test% |
+------+-------+
1 row in set (0.05 sec)

testB値で終わるtestAテーブルから値を取得するにはどうすればよいですか?

このクエリ:

SELECT tA.testX
FROM testA tA
JOIN testB tB USING (id)
WHERE tA.testX LIKE CONCAT('%', tB.testY);

結果全体を返します:

+-----------------+
| testX           |
+-----------------+
| this is a test% |
| a test% this is |
+-----------------+
2 rows in set (0.04 sec)

CONCAT('%', tB.testY)'%test%'を返すので、これはもちろん論理に聞こえます。しかし、私のアプリでは、LIKEの右側に複雑な値があり、関数呼び出しと列と文字列の組み合わせが含まれています。

4

1 に答える 1

1

私はついに私の解決策を見つけました:

SELECT tA.testX
FROM testA tA JOIN testB tB USING (id)
WHERE RIGHT(tA.testX, LENGTH(tB.testY)) = tB.testY

ください :

+-----------------+
| testX           |
+-----------------+
| this is a test% |
+-----------------+

あなたはそれを行うことができます:

  • 値()で始まる文字列が必要な場合はLEFTLIKE 'xxx%'
  • LIKE '%xxx'値( )で終わる文字列を検索する必要がある場合は
  • LIKE '%xxx%'値( )を含む文字列を検索する必要がある場合はLOCATE

誰かがそれが役に立つと思う場合に備えて、私はそれを投稿します。

于 2012-10-30T18:05:57.727 に答える