在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
作为一个表达式使用的子查询返回了多列: 在查询中,我们需要以第2条查询语句作为第一条查询语句的条件,但是第一条根据这个条件查询出来是多个数据,这时候又需要保留多个数据,运用子查询就会报错, 以下就为解决这种多对多关系查询,且没有关联关系的表的解决方案: select c.rain_value,c.ad_nm from ( select *, json::json->t2.lon_lat as rain_value from actual_time_model_json t1, (SELECT DISTINCT lon || '_' || lat as lon_lat,ad_nm from grid_all_points_null)t2 where section='0' and t1.filename = 'Z_NWGD_C_BCCD_20180711022009_P_RFFC_SPCC-ER01_201807110800_02401.GRB2' )c where c.rain_value is not null 补充:PostgreSQL 的子查询 相关的知识 ,exists,any,all SubquerySELECT film_id, title, rental_rate FROM film WHERE rental_rate > ( SELECT AVG (rental_rate) FROM film ); 有了子查询,在设定 需要查询表才能得到 查询条件时,就可以 直接 在一条语句中 写,不用分开多条写了,方便了许多。 子查询返回多条时,可以在 where 子句中 用 IN,来匹配查询条件。 SELECT film_id, title FROM film WHERE film_id IN ( SELECT inventory.film_id FROM rental INNER JOIN inventory ON inventory.inventory_id = rental.inventory_id WHERE return_date BETWEEN '2005-05-29' AND '2005-05-30' ); EXISTS 操作符在 where 子句的 查询条件中,exists 操作符,会在子查询有返回行时,返回true;不论返回几行。 因此,子查询中的查询字段仅写1就好;标准的写法:EXISTS (SELECT 1 FROM tbl WHERE condition) SELECT first_name, last_name FROM customer WHERE EXISTS ( SELECT 1 FROM payment WHERE payment.customer_id = customer.customer_id ); NO EXISTS ,与之相反,当子查询返回0行时,返回true SELECT first_name, last_name FROM customer c WHERE NOT EXISTS (SELECT 1 FROM payment p WHERE p.customer_id = c.customer_id AND amount > 11 ) ORDER BY first_name, last_name; 当子查询返回 NULL,会返回true, 也就是返回所有行。 SELECT first_name, last_name FROM customer WHERE EXISTS( SELECT NULL ) ORDER BY first_name, last_name; ANY与任何子查询返回的 值 匹配就 返回 true expresion operator ANY(subquery) 表达式一般为 字段 操作符为 >,<,=,<>,>=,<= ANY 可以与 SOME 替换 子查询 返回的 必须是 一列, SELECT title FROM film WHERE length >= ANY( SELECT MAX( length ) FROM film INNER JOIN film_category USING(film_id) GROUP BY category_id ); The = ANY is equivalent to IN operator. Note that the <> ANY operator is different from NOT IN. The following expression: x <> ANY (a,b,c) is equivalent to x <> a OR x <> b OR x <> c ALL所有子查询返回的值 匹配 则 返回 true 也就是 大于最大,小于最小 SELECT film_id, title, length FROM film WHERE length > ALL ( SELECT ROUND(AVG (length),2) FROM film GROUP BY rating ) ORDER BY length; 以上为个人经验,希望能给大家一个参考,也希望大家多多支持极客世界。如有错误或未考虑完全的地方,望不吝赐教。 |
请发表评论