Snowpark Migration Accelerator: Where¶
描述¶
根据指定条件筛选查询或子查询返回的数据。(Databricks SQL 语言参考 WHERE (https://docs.databricks.com/en/sql/language-manual/sql-ref-syntax-qry-select-where.html))
WHERE
子句通过定义必须满足的特定条件来筛选数据。(Snowflake SQL 语言参考 WHERE)
语法¶
WHERE <boolean_expression>
...
WHERE <predicate>
[ ... ]
示例源模式¶
设置数据¶
Databricks¶
CREATE TABLE person (id INT, name STRING, age INT);
INSERT INTO person VALUES
(100, 'John', 30),
(200, 'Mary', NULL),
(300, 'Mike', 80),
(400, 'Dan' , 50);
Snowflake¶
CREATE TABLE person (id INT, name STRING, age INT);
INSERT INTO person VALUES
(100, 'John', 30),
(200, 'Mary', NULL),
(300, 'Mike', 80),
(400, 'Dan' , 50);
模式代码¶
Databricks¶
-- 1. Comparison operator in `WHERE` clause.
SELECT * FROM person WHERE id > 200 ORDER BY id;
-- 2. Comparison and logical operators in `WHERE` clause.
SELECT * FROM person WHERE id = 200 OR id = 300 ORDER BY id;
-- 3. IS NULL expression in `WHERE` clause.
SELECT * FROM person WHERE id > 300 OR age IS NULL ORDER BY id;
-- 4. Function expression in `WHERE` clause.
SELECT * FROM person WHERE length(name) > 3 ORDER BY id;
-- 5. `BETWEEN` expression in `WHERE` clause.
SELECT * FROM person WHERE id BETWEEN 200 AND 300 ORDER BY id;
-- 6. Scalar Subquery in `WHERE` clause.
SELECT * FROM person WHERE age > (SELECT avg(age) FROM person);
-- 7. Correlated Subquery in `WHERE` clause.
SELECT * FROM person AS parent
WHERE EXISTS (SELECT 1 FROM person AS child
WHERE parent.id = child.id
AND child.age IS NULL);
在
WHERE
子句中使用比较运算符(如 =、>、<, >=、<=)来筛选数据。
ID |
NAME |
AGE |
---|---|---|
300 |
Mike |
80 |
400 |
Dan |
50 |
使用比较运算符(=、<, >、<=, >=、!=)和
WHERE
子句中的逻辑运算符(AND、OR、NOT)来筛选数据。
ID |
NAME |
AGE |
---|---|---|
200 |
Mary |
null |
300 |
Mike |
80 |
在
WHERE
子句中使用IS NULL
来检查空值。
ID |
NAME |
AGE |
---|---|---|
200 |
Mary |
null |
400 |
Dan |
50 |
在
WHERE
子句中使用函数表达式。
ID |
NAME |
AGE |
---|---|---|
100 |
John |
30 |
200 |
Mary |
null |
300 |
Mike |
80 |
在
WHERE
子句中使用BETWEEN
运算符根据一系列值筛选数据。
ID |
NAME |
AGE |
---|---|---|
200 |
Mary |
null |
300 |
Mike |
80 |
在
WHERE
子句中使用标量子查询。
ID |
NAME |
AGE |
---|---|---|
300 |
Mike |
80 |
WHERE
子句中的一个子查询,它参考外部查询中的列。
ID |
NAME |
AGE |
---|---|---|
200 |
Mary |
null |
Snowflake¶
-- 1. Comparison operator in `WHERE` clause.
SELECT * FROM person WHERE id > 200 ORDER BY id;
-- 2. Comparison and logical operators in `WHERE` clause.
SELECT * FROM person WHERE id = 200 OR id = 300 ORDER BY id;
-- 3. IS NULL expression in `WHERE` clause.
SELECT * FROM person WHERE id > 300 OR age IS NULL ORDER BY id;
-- 4. Function expression in `WHERE` clause.
SELECT * FROM person WHERE length(name) > 3 ORDER BY id;
-- 5. `BETWEEN` expression in `WHERE` clause.
SELECT * FROM person WHERE id BETWEEN 200 AND 300 ORDER BY id;
-- 6. Scalar Subquery in `WHERE` clause.
SELECT * FROM person WHERE age > (SELECT avg(age) FROM person);
-- 7. Correlated Subquery in `WHERE` clause.
SELECT * FROM person AS parent
WHERE EXISTS (SELECT 1 FROM person AS child
WHERE parent.id = child.id
AND child.age IS NULL);
在
WHERE
子句中使用比较运算符(如 =、>、<, >=、<=)来筛选数据。
ID |
NAME |
AGE |
---|---|---|
300 |
Mike |
80 |
400 |
Dan |
50 |
在
WHERE
子句中使用比较运算符(如 =、<, >、<=, >=)和逻辑运算符(如 AND、OR、NOT)来筛选数据。
ID |
NAME |
AGE |
---|---|---|
200 |
Mary |
null |
300 |
Mike |
80 |
在
WHERE
子句中使用IS NULL
来检查空值。
ID |
NAME |
AGE |
---|---|---|
200 |
Mary |
null |
400 |
Dan |
50 |
在
WHERE
子句中使用函数表达式。
ID |
NAME |
AGE |
---|---|---|
100 |
John |
30 |
200 |
Mary |
null |
300 |
Mike |
80 |
在
WHERE
子句中使用BETWEEN
运算符根据一系列值筛选数据。
ID |
NAME |
AGE |
---|---|---|
200 |
Mary |
null |
300 |
Mike |
80 |
在
WHERE
子句中使用标量子查询。
ID |
NAME |
AGE |
---|---|---|
300 |
Mike |
80 |
WHERE
子句中的关联子查询。
ID |
NAME |
AGE |
---|---|---|
200 |
Mary |
null |
已知问题¶
未发现任何问题