1

MySQL を使用して、次のようないくつかのフィールドで結果を検索するエンジンを作成しています。

SELECT table1.*, table2.* FROM table1
JOIN table2
ON table1.id = table2.tid
WHERE 
(table1.field1 LIKE 'keyword%' OR table1.field2 LIKE 'keyword%')
AND
(table2.field1 LIKE 'keyword%' OR table2.field2 LIKE 'keyword%')
ORDER BY table1.id ASC;

しかし、どのフィールドがキーワードに一致するかを知る必要があります...どうすればそれを行うことができますか?

ところで、私はPHPを使用しています。

4

4 に答える 4

2

未確認:

SELECT table1.*, 
       table2.*, 
       case (table2.field1 LIKE 'keyword%' and table1.field1 LIKE 'keyword%')
           when 1 
           then 'f1' 
           else 'f2' 
       end as field
FROM table1
...
于 2012-04-13T16:11:45.413 に答える
1

あなたができる

SELECT table1.*, table2.*, 'table1' as `target` as FROM table1
JOIN table2
ON table1.id = table2.tid
WHERE 
(table1.field1 LIKE 'keyword%' OR table1.field2 LIKE 'keyword%')
UNION ALL 
SELECT table1.*, table2.*,'table2' as target FROM table1
JOIN table2
ON table1.id = table2.tid
WHERE 
(table2.field1 LIKE 'keyword%' OR table2.field2 LIKE 'keyword%')

target結果を取得するときに table1 または table2かどうかを確認します

于 2012-04-13T16:10:24.577 に答える
1

条件に列を追加できます

SELECT table1.*, table2.*, 
table1.field1 LIKE 'keyword%' AS Table1Field1Match, 
table1.field2 LIKE 'keyword%' AS Table1Field2Match,
table2.field1 LIKE 'keyword%' AS Table2Field1Match, 
table2.field2 LIKE 'keyword%' AS Table2Field2Match
FROM table1
JOIN table2
ON table1.id = table2.tid
WHERE 
(table1.field1 LIKE 'keyword%' OR table1.field2 LIKE 'keyword%')
AND
(table2.field1 LIKE 'keyword%' OR table2.field2 LIKE 'keyword%')
ORDER BY table1.id ASC;
于 2012-04-13T16:11:04.867 に答える
0

あなたのループでは、これを行うことができます:

Table X
Field1      Field2
-------------------
boat        caca
hello       boat

$Keyword = 'boat';
while($Res = blabla){
    if(strpos($Res['Field1'], $Keyword) !== false){
        echo 'String found in Field 1<br />';
    }

    if(strpos($Res['Field2'], $Keyword) !== false){
        echo 'String found in Field 2<br />';
    }
}

// Will output
String found in Field 1<br />
String found in Field 2<br />
于 2012-04-13T16:10:46.230 に答える