SQL:选择具有特定值的行,独立于列

SQL:选择具有特定值的行,独立于列,sql,vertica,rowfilter,Sql,Vertica,Rowfilter,我在vertica DB中有以下选项卡: +-----+-------+-------+-------+ | tid | Item1 | Item2 | Item3 | +-----+-------+-------+-------+ | 1 | A | B | C | | 2 | B | D | A | | 3 | C | D | A | | 4 | D | B | A | +----

我在vertica DB中有以下选项卡:

+-----+-------+-------+-------+
| tid | Item1 | Item2 | Item3 |
+-----+-------+-------+-------+
|   1 | A     | B     | C     |
|   2 | B     | D     | A     |
|   3 | C     | D     | A     |
|   4 | D     | B     | A     |
+-----+-------+-------+-------+
我希望找到包含项目A和B的所有行,从而生成一个表:

+-----+-------+-------+-------+
| tid | Item1 | Item2 | Item3 |
+-----+-------+-------+-------+
|   1 | A     | B     | C     |
|   2 | B     | D     | A     |
|   4 | D     | B     | A     |
+-----+-------+-------+-------+
A和B的出现顺序是随机的。这也是一个抽象的例子,我可能想寻找四个项目中的三个,甚至一个项目。是否有一种简单的方法可以检查每一行是否包含与订单无关的某些项目?

尝试以下方法:

select      *

from        mytable

where       'A' in (Item1,Item2,Item3)
        and 'B' in (Item1,Item2,Item3)
 SELECT * FROM TAB1 WHERE (ITEM1 IN ('A','B') AND ITEM2 IN ('A','B')) 
                            OR (ITEM1 IN ('A','B') AND ITEM3 IN ('A','B')) 
                            OR (ITEM2 IN ('A','B') AND ITEM2 IN ('A','B'))
试试这个:

 SELECT * FROM TAB1 WHERE (ITEM1 IN ('A','B') AND ITEM2 IN ('A','B')) 
                            OR (ITEM1 IN ('A','B') AND ITEM3 IN ('A','B')) 
                            OR (ITEM2 IN ('A','B') AND ITEM2 IN ('A','B'))

另一种方法可能是将列连接成字符串,并使用INSTR搜索该列:

选择a.tid,a.项目 从…起 选择tid、CONCATItem1、Item2、Item3作为项目 从MyTable作为 哪里 仪表项目,A>0 和 仪表项目,B>0 等
CONCAT和WHERE条件中的列可以根据需要扩展

另一种方法可能是将列连接成字符串,并使用INSTR搜索该列:

mysql> select * from vertica where (item1 in("A") and (item2 in("B") or item3 in
("B"))) or (item1 in("B") and (item2 in("A") or item3 in("A"))) or (item2 in("A"
) and (item1 in("B") or item3 in("B"))) or (item2 in("B") and (item1 in("A") or
item3 in("A"))) or (item3 in("A") and (item1 in("B") or item2 in("B"))) or (item
3 in("B") and (item1 in("A") or item2 in("A")));

O/P ->
+-----+-------+-------+-------+
| tid | item1 | item2 | item3 |
+-----+-------+-------+-------+
|   1 | A     | B     | C     |
|   2 | B     | D     | A     |
|   4 | D     | B     | A     |
+-----+-------+-------+-------+
3 rows in set (0.02 sec)
选择a.tid,a.项目 从…起 选择tid、CONCATItem1、Item2、Item3作为项目 从MyTable作为 哪里 仪表项目,A>0 和 仪表项目,B>0 等
CONCAT和WHERE条件中的列可以根据需要扩展

是的,它们都可以工作。我想跳过列的枚举,因为我只想扫描整行,但似乎没有优雅的方法。是的,它们都能工作。我想跳过列的枚举,因为我只想扫描整行,但似乎没有优雅的方法。
mysql> select * from vertica where (item1 in("A") and (item2 in("B") or item3 in
("B"))) or (item1 in("B") and (item2 in("A") or item3 in("A"))) or (item2 in("A"
) and (item1 in("B") or item3 in("B"))) or (item2 in("B") and (item1 in("A") or
item3 in("A"))) or (item3 in("A") and (item1 in("B") or item2 in("B"))) or (item
3 in("B") and (item1 in("A") or item2 in("A")));

O/P ->
+-----+-------+-------+-------+
| tid | item1 | item2 | item3 |
+-----+-------+-------+-------+
|   1 | A     | B     | C     |
|   2 | B     | D     | A     |
|   4 | D     | B     | A     |
+-----+-------+-------+-------+
3 rows in set (0.02 sec)