如何获得结果集的交点?

问题描述

结果集1:

select code from quote where cond1

结果集2:

select code from vix  where cond2

我想要一个包含code的集合,该集合是Result set1Result set2的交集。

select code from quote where cond1 and code in (select code from vix  where cond2);

它不起作用!GMB和Akina的答案均显示相同的错误信息。

RROR 1267(HY000):操作'='的排序规则(utf8mb4_general_ci,IMPLICIT)和(utf8mb4_unicode_ci,IMPLICIT)的非法混合

让我们显示表结构:

show create table quote;
+-------+------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                                                                                                                                                             |
+-------+-----------------------------------------------------------------
| quote | CREATE TABLE `quote` (
  `id` int(8) unsigned NOT NULL AUTO_INCREMENT,`code` text COLLATE utf8mb4_unicode_ci,`date` date DEFAULT NULL,`open` double DEFAULT NULL,`high` double DEFAULT NULL,`low` double DEFAULT NULL,`close` double DEFAULT NULL,`volume` bigint(20) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=17173979 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |
+-------+--------------------------------------------------------------------+

show create table vix;
+-------+-------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                  |
+-------+--------------------------------------------------------------------+
| vix   | CREATE TABLE `vix` (
  `code` text,`span` smallint(6) DEFAULT NULL,`vix` double DEFAULT NULL,`extrem_date` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |

从表quotevix中设置的搜索代码

select code from quote where volume > 10000000 and date='2020-08-24';
+-------+
| code  |
+-------+
| zom   |
| ibio  |

注意:大约120行。

select code from vix  where span >30;
+---------+
| code    |
+---------+
| canf    |
| ensv    |

注意:大约1100行。 获取两个结果集的交集:

MariaDB [stock]> select code 
    -> from quote q 
    -> where 
    ->     volume > 10000000 and date='2020-08-24'
    ->     and exists (select 1 from vix v where span >30 and (v.code = q.code));
ERROR 1267 (HY000): Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_unicode_ci,IMPLICIT) for operation '='

解决方法

您的(伪)查询查询应该执行您想要的操作。但是,我发现,当值列表变大时,使用exists会更有效。我可以这样说:

select code 
from quote q 
where 
    cond1
    and exists (select 1 from vix v where cond2 and v.code = q.code)
,
SELECT code 
FROM (select code from quote where cond1) q1
JOIN (select code from vix   where cond2) q2 USING (code)
,
// iterate over all files in the 'templates' folder INSIDE the .exe
each(fs.readdirSync('templates'),(filename: string) => {
  const dataBuffer = fs.readFileSync(`templates/${filename}`);
  // do sth with that file data,e.g. export it to some location (outside the .exe)
  const stream = fs.createWriteStream(`${outDir}/${filename}`);
  stream.write(dataBuffer );
  stream.close();
});