0

このようなフィールドを持つテーブルがあります

category
--------------
20,14,13,16,19

そして、私はこの文字列を持っています:

20,16,9,5

このカテゴリーの商品を探したい

mysqlクエリで。

誰かがアイデアを持っていますか?

4

4 に答える 4

0

カンマ区切りの値を照合できます。

ここにMysqlクエリがあります

SELECT tens.d*10 + ones.d AS n
FROM (SELECT 0 AS d UNION ALL
SELECT 1 AS d UNION ALL
SELECT 2 AS d UNION ALL
SELECT 3 AS d UNION ALL
SELECT 4 AS d UNION ALL
SELECT 5 AS d UNION ALL
SELECT 6 AS d UNION ALL
SELECT 7 AS d UNION ALL
SELECT 8 AS d UNION ALL
SELECT 9 AS d) AS tens
INNER JOIN
(SELECT 0 AS d UNION ALL
SELECT 1 AS d UNION ALL
SELECT 2 AS d UNION ALL
SELECT 3 AS d UNION ALL
SELECT 4 AS d UNION ALL
SELECT 5 AS d UNION ALL
SELECT 6 AS d UNION ALL
SELECT 7 AS d UNION ALL
SELECT 8 AS d UNION ALL
SELECT 9 AS d) AS ones
WHERE (tens.d*10 + ones.d) IN (20,14,13,16,19)
AND (tens.d*10 + ones.d) IN ( 20,16,9,5 )
ORDER BY n
于 2014-04-03T11:47:18.580 に答える
0
$myString = '20,16,9,5';

$sets = array();
foreach(explode(',', $myString) as $individualValue) {
    $sets[] = 'FIND_IN_SET(' . $individualValue. ', category) > 0';
}
$whereClause = implode(' OR ', $sets);

$myQuery = 'SELECT * FROM mytable';
if ($whereClause > '') {
    $myQuery .= ' WHERE ' . $whereClause;
}
于 2014-04-03T11:55:44.550 に答える