SQL: Select 具有特定值的行,独立于列
SQL: Select rows that have specfic values, independent from the column
我在 vertica 数据库中有以下表格:
+-----+-------+-------+-------+
| tid | Item1 | Item2 | Item3 |
+-----+-------+-------+-------+
| 1 | A | B | C |
| 2 | B | D | A |
| 3 | C | D | A |
| 4 | D | B | A |
+-----+-------+-------+-------+
我想查找包含项目 A
和 B
的所有行,结果是 table:
+-----+-------+-------+-------+
| 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'))
另一种方法可能是将列连接成一个字符串,然后使用 INSTR 搜索该列:
SELECT a.tid, a.Items
FROM
(SELECT tid, CONCAT(Item1, Item2, Item3) AS Items
FROM MyTable) AS a
WHERE
INSTR(a.Items, "A") > 0
AND
INSTR(a.Items, "B") > 0
etc.
可以根据需要扩展 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)
我在 vertica 数据库中有以下表格:
+-----+-------+-------+-------+
| tid | Item1 | Item2 | Item3 |
+-----+-------+-------+-------+
| 1 | A | B | C |
| 2 | B | D | A |
| 3 | C | D | A |
| 4 | D | B | A |
+-----+-------+-------+-------+
我想查找包含项目 A
和 B
的所有行,结果是 table:
+-----+-------+-------+-------+
| 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'))
另一种方法可能是将列连接成一个字符串,然后使用 INSTR 搜索该列:
SELECT a.tid, a.Items
FROM
(SELECT tid, CONCAT(Item1, Item2, Item3) AS Items
FROM MyTable) AS a
WHERE
INSTR(a.Items, "A") > 0
AND
INSTR(a.Items, "B") > 0
etc.
可以根据需要扩展 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)