作为一个表达式使用的子查询返回了多列:
在查询中,我们需要以第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
subquery
select 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;
以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。如有错误或未考虑完全的地方,望不吝赐教。