2

我正在对具有 6 个不同列的数据库进行自定义搜索。为了进行搜索,我有 6 个只接受数字的不同输入字段。

我已经让 6 列搜索工作了,代码:

SELECT * FROM data 
WHERE 1_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a')
AND 2_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a')
AND 3_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a')
AND 4_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a')
AND 5_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a')
AND 6_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a')
ORDER BY id ASC;

尽管输入的顺序,这给出了正确的结果。

但是我需要选择来给我结果,无论它是否匹配输入的 4 个或 5 个数字。我尝试了 OR,但我不知道如何定义匹配的列数。

这可能吗?

感谢关注!

编辑: - - - - - - - - - -

我被 dbaseman 提供的代码卡住了。

因为我得到了一个真实的结果,但我真的不知道它是通过匹配 4、5 还是 6 列生成的。

我想在之后插入一个 CASE ,但不起作用。

有没有办法知道哪个列组合给了我结果?

想到类似(粗略的)if = 4,然后是 result-4,else if = 5,然后是 result-5 ......等等......然后能够将行放入数组中。

谢谢!

4

1 回答 1

4

而不是使用和/或,总结命中:

SELECT *,
    CASE WHEN 1_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_1,
    CASE WHEN 2_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_2,
    CASE WHEN 3_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_3,
    CASE WHEN 4_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_4,
    CASE WHEN 5_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_5,
    CASE WHEN 6_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_6,

    CASE WHEN 1_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
    + CASE WHEN 2_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
    + CASE WHEN 3_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
    + CASE WHEN 4_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
    + CASE WHEN 5_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
    + CASE WHEN 6_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
    AS no_of_matches
FROM data 
WHERE 
    (
    CASE WHEN 1_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
    + CASE WHEN 2_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
    + CASE WHEN 3_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
    + CASE WHEN 4_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
    + CASE WHEN 5_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
    + CASE WHEN 6_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END
    ) in (4,5,6)
ORDER BY id ASC;

替代,使用嵌套查询更漂亮:

SELECT a.*, a.match_1+a.match_2+a.match_3+a.match_4+a.match_5+a.match_6 AS no_of_matches
FROM (
    SELECT *,
        CASE WHEN 1_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_1,
        CASE WHEN 2_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_2,
        CASE WHEN 3_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_3,
        CASE WHEN 4_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_4,
        CASE WHEN 5_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_5,
        CASE WHEN 6_column IN ('$a1a','$a2a','$a3a','$a4a','$a5a','$a6a') THEN 1 ELSE 0 END AS match_6
        FROM data 
    ) a
WHERE a.match_1+a.match_2+a.match_3+a.match_4+a.match_5+a.match_6 in (4,5,6)
ORDER BY id ASC;
于 2012-04-26T23:16:48.483 回答