有没有一种方法可以使用一个MySQL查询合并两个表,并覆盖和乘以结果

有没有一种方法可以使用一个MySQL查询合并两个表,并覆盖和乘以结果,mysql,sql,Mysql,Sql,我有三个表,第一个表存储大陆,第二个表存储默认存在的动物,第三个表存储第二个表中特定大陆和动物的例外情况 大陆限制为3以简化结果 | continent_code | |----------------| | EU | | OC | | AN | 动物默认存在 | animal_id | animal | presence | |-----------|------------|----------| | 1

我有三个表,第一个表存储大陆,第二个表存储默认存在的动物,第三个表存储第二个表中特定大陆和动物的例外情况

大陆限制为3以简化结果

| continent_code |
|----------------|
| EU             |
| OC             |
| AN             |
动物默认存在

| animal_id | animal     | presence |
|-----------|------------|----------|
| 1         | dog        | 1        |
| 2         | kangaroo   | 0        |
| 3         | polar bear | 0        |
大陆动物存在例外

| continent_code | animal_id | presence |
|----------------|-----------|----------|
| OC             | 2         | 1        |
| AN             | 1         | 0        |
| AN             | 3         | 1        |
结果是对生活在那里的所有大陆和动物的总结:

| continent_code | animal_id |
|----------------|-----------|
| EU             | 1         |
| OC             | 1         |
| OC             | 2         |
| AN             | 3         |
我能用一个MySQL查询得到这样的结果吗?

首先,需要在动物和大陆之间进行交叉连接,以获得它们的所有可能组合。现在,您可以基于动物和大陆进行异常表的左连接

最终,一个稍微复杂的条件可以用来过滤出所需的结果集。我们可以考虑缺省值为1的那些行,并且它们没有例外,或者缺省存在为0的那些行,但是对于它们定义了异常。
SELECT c.continent_code,
       cape.animal_id
FROM   (SELECT c.continent_code,
               a.animal_id,
               a.presence AS default_presence
        FROM   animals_default_presence AS a
               CROSS JOIN continents AS c
              ) AS all_combos
       LEFT JOIN continent_animals_presence_exceptions AS cape
              ON cape.continent_code = default_present_combos.continent_code
                 AND cape.animal_id = default_present_combos.animal_id
WHERE  ( all_combos.default_presence = 1
         AND ( cape.presence = 1
                OR cape.presence IS NULL ) )
        OR ( all_combos.default_presence = 0
             AND cape.presence = 1 ) 
使用UNION ALL的早期版本:

一种方法是利用所有人。在其中一个SELECT查询中,您可以获得动物代码和欧洲大陆代码的所有组合,其中动物的默认状态为1。但是,我们需要使用左连接进行反连接。。哪里为NULL以消除在特定大陆上默认动物的存在被定义为0的异常

在另一个SELECT查询中,我们只需获取默认状态为0的动物的组合,但它们在某些大陆上的状态为1,如异常中所定义

SELECT 
  c.continent_code, cape.animal_id 
FROM 
  (SELECT c.continent_code, a.animal_id 
   FROM animals_default_presence AS a 
   CROSS JOIN continents AS c 
   WHERE a.presence = 1) AS default_present_combos 
   LEFT JOIN continent_animals_presence_exceptions AS cape 
          ON cape.continent_code = default_present_combos.continent_code 
             AND cape.animal_id = default_present_combos.animal_id 
             AND cape.presence = 0 
WHERE cape.animal_id IS NULL

UNION ALL 

SELECT 
  cape.continent_code, cape.animal_id 
FROM animals_default_presence AS a 
JOIN continent_animals_presence_exceptions AS cape 
  ON cape.animal_id = a.animal_id 
     AND cape.presence = 1 
WHERE a.presence = 0 
你可以使用联合所有。我认为以下是您所需要的:

select c.continent_code, adp.animal_id
from continent c cross join
     animals_default_presence adp 
where adp.presence = 1 and
      (c.continent_code, adp.animal_id) not in 
            (select cape.continent_code, cape.animal_id
             from continent_animals_presence_exceptions cape
             where cape.presence = 0
            )
union all
select cape.continent_code, cape.animal_id
from continent_animals_presence_exceptions cape
where cape.presence = 1;

是一把小提琴。

是的,你可以。您需要一些连接,可能还需要一个CASE语句来处理这两个presence字段。你试过什么吗?另外,如果您的表和示例数据已经存在,您可以复制您的CREATE TABLE并从MySQL服务器生成一些INSERT语句,这将使人们更容易尝试查询,从而能够帮助您。谢谢