如何获取结果集的交集?
How to get the result set's intersection?
结果集1:
select code from quote where cond1
结果集2:
select code from vix where cond2
我想要一个包含 code
的集合,它是 Result set1
和 Result 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) 用于操作 '='
让我们显示 table 结构:
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 |
从 table quote
和 vix
.
中搜索代码集
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)
select code
from quote q
where
cond1
and exists (select 1 from vix v where cond2 and (v.code collate utf8mb4_unicode_ci = q.code))
结果集1:
select code from quote where cond1
结果集2:
select code from vix where cond2
我想要一个包含 code
的集合,它是 Result set1
和 Result 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) 用于操作 '='
让我们显示 table 结构:
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 |
从 table quote
和 vix
.
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)
select code
from quote q
where
cond1
and exists (select 1 from vix v where cond2 and (v.code collate utf8mb4_unicode_ci = q.code))