1

I want two lists, that both show records from their respective tables that are not being used another table.

Two lists, one for the table a_aif and one for the table a_proxy. The list to show only those fee_source_id's that are not present in the column a_fees.fee_source

Here is what I have so far (two separate queries). Any way to make a view with these two lists in separate columns?

SELECT a_aif.fee_source_id
FROM a_aif, a_fees
WHERE a_fees.fee_source NOT IN (SELECT a_aif.fee_source_id);

SELECT a_proxy.fee_source_id
FROM a_proxy, a_fees
WHERE a_fees.fee_source NOT IN (SELECT a_proxy.fee_source_id);
4

1 回答 1

0

这种方法对于您正在使用的 RDBMS 应该是非常不可知的。

LEFT JOIN您可以从中选择右侧所在的记录的一对s NULLUNION查询用于从 和 表中构建 id 的主a_aif列表a_proxy

SELECT 
  full_list.fee_source_id,
  CASE WHEN aif_fee_source_id IS NOT NULL THEN 'present' ELSE 'not-present' END AS aif,
  CASE WHEN proxy_fee_source_id IS NOT NULL THEN 'present' ELSE 'not-present' END AS proxy
FROM (
  /* UNION list gets total list of ids from both tables */
  SELECT fee_source_id FROM a_aif
  UNION SELECT fee_source_id FROM a_proxy
) full_list
LEFT JOIN (
  /* First subquery joins to get ids not present in a_aif */
  SELECT fee_source_id AS aif_fee_source_id
  FROM
    a_aif
    LEFT JOIN a_fees ON a_aif.fee_source_id = a_fees.fee_source
) aif ON full_list.fee_source_id = aif_fee_source_id
LEFT JOIN (
  /* Second subquery joins to get ids not present in a_proxy */
  SELECT fee_source_id AS proxy_fee_source_id
  FROM
    a_proxy
    LEFT JOIN a_fees ON a_proxy.fee_source_id = a_fees.fee_source
) proxy ON full_list.fee_source_id = proxy.proxy_fee_source_id

http://sqlfiddle.com/#!2/cc170/3

于 2012-12-22T02:31:07.140 回答