如何为集合匹配编写选择查询

时间:2014-02-13 06:21:46

标签: mysql sql

请帮我解决问题。 我无法考虑子查询

SELECT name FROM client其中assest_id IN(?subquery?)

enter image description here

1 个答案:

答案 0 :(得分:2)

第一个问题

select name from client where asset_id in(
select asset_id from client_asset where
asset_desc='2'
intersect
select asset_id from client_asset where
asset_id not in (select asset_id from client_asset where
                 asset_name not in ('apartment','byke')));

对于第一个问题,我首先使用2room获取了asset_id,并使用仅intersected作为资产的asset_id获取了byke and apartment。这被用作includement语句的子查询来获取asset_id

第二个问题

select name from client where asset_id in
(select asset_id from client_asset where
asset_desc>='2' and asset_name='apartment'
intersect
select asset_id from client_asset where
asset_name='car');

此查询是自我解释的,并且基于类似的理由