带有 RIGHT JOIN 的 CROSS JOIN
A CROSS JOIN with a RIGHT JOIN
我有一个 table "typl" 只包含 2 个值:
typl
------
COL
IND
和一个 table "store" 有 2 列:
achl | typl
------+------
AAAA | IND
AAAA | IND
AAAA | IND
AAAA | IND
AAAA | IND
AAAA | IND
BBBB | COL
BBBB | COL
BBBB | IND
BBBB | IND
BBBB | IND
BBBB | IND
BBBB | IND
BBBB | IND
BBBB | IND
问题:我可以找到一个查询,对于商店中的一组 achl table,如果只有一个 typl,比如 achl='AAAA',所有的行都会交叉加入 "typl" table,所以结果将是:
achl | typl
------+------
AAAA | COL
AAAA | COL
AAAA | COL
AAAA | COL
AAAA | COL
AAAA | COL
AAAA | IND
AAAA | IND
AAAA | IND
AAAA | IND
AAAA | IND
AAAA | IND
BBBB | COL
BBBB | COL
BBBB | IND
BBBB | IND
BBBB | IND
BBBB | IND
BBBB | IND
BBBB | IND
BBBB | IND
SELECT
*
FROM store
WHERE achl IN (
SELECT achl
FROM store
GROUP BY achl
HAVING COUNT(DISTINCT typl) = 1
)
select *
from (
select achl, typl
from store
union all
select achl, t.typl
from store s cross join typl t
where (achl, t.typl) not in (
select achl, typl
from store
)
) s
order by achl, typl
我有一个 table "typl" 只包含 2 个值:
typl
------
COL
IND
和一个 table "store" 有 2 列:
achl | typl
------+------
AAAA | IND
AAAA | IND
AAAA | IND
AAAA | IND
AAAA | IND
AAAA | IND
BBBB | COL
BBBB | COL
BBBB | IND
BBBB | IND
BBBB | IND
BBBB | IND
BBBB | IND
BBBB | IND
BBBB | IND
问题:我可以找到一个查询,对于商店中的一组 achl table,如果只有一个 typl,比如 achl='AAAA',所有的行都会交叉加入 "typl" table,所以结果将是:
achl | typl
------+------
AAAA | COL
AAAA | COL
AAAA | COL
AAAA | COL
AAAA | COL
AAAA | COL
AAAA | IND
AAAA | IND
AAAA | IND
AAAA | IND
AAAA | IND
AAAA | IND
BBBB | COL
BBBB | COL
BBBB | IND
BBBB | IND
BBBB | IND
BBBB | IND
BBBB | IND
BBBB | IND
BBBB | IND
SELECT
*
FROM store
WHERE achl IN (
SELECT achl
FROM store
GROUP BY achl
HAVING COUNT(DISTINCT typl) = 1
)
select *
from (
select achl, typl
from store
union all
select achl, t.typl
from store s cross join typl t
where (achl, t.typl) not in (
select achl, typl
from store
)
) s
order by achl, typl