您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何选择所有在mysql中找不到输入数组的数据

如何选择所有在mysql中找不到输入数组的数据

辅助表用于左联接/右联接概念,但并不是那么简单。

从我的答案在这里(Edit3)在这里

CREATE TABLE 4kTable
(   -- a helper table of about 4k consecutive ints
    id int auto_increment primary key,
    thing int null
)engine=MyISAM;

insert 4kTable (thing) values (null),(null),(null),(null),(null),(null),(null),(null),(null);
insert 4kTable (thing) select thing from 4kTable;
insert 4kTable (thing) select thing from 4kTable;
insert 4kTable (thing) select thing from 4kTable;
insert 4kTable (thing) select thing from 4kTable;
insert 4kTable (thing) select thing from 4kTable;
insert 4kTable (thing) select thing from 4kTable;
insert 4kTable (thing) select thing from 4kTable;
insert 4kTable (thing) select thing from 4kTable;
insert 4kTable (thing) select thing from 4kTable;
-- verify:
-- select min(id),max(id),count(*) from 4kTable;
-- 1 4608 4608

ALTER TABLE 4kTable ENGINE = InnoDB; -- *********** it is Now InnoDB

用户fthiella修改后的答案中 …此处发布

select SUBSTRING_INDEX(SUBSTRING_INDEX(@str, ',', 4k.id), ',', -1) name 
from 
  4kTable 4k  
  cross join (select @str:='SMITH,WARD,KING,TOM') vars 
  on CHAR_LENGTH(@str) 
     -CHAR_LENGTH(REPLACE(@str, ',', ''))>=4k.id-1; 
+-------+
| name  |
+-------+
| SMITH |
| WARD  |
| KING  |
| TOM   |
+-------+

因此,以上是将csv放入查询并从中生成表的一般形式。

现在d,根据上述内容创建一个派生表(),并通过RIGHT JOIN与操作码结合(该模式已在操作码中显示

select d.name as rtable_name,e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm,e.deptno 
from emp e 
right join 
(   select SUBSTRING_INDEX(SUBSTRING_INDEX(@str, ',', 4k.id), ',', -1) name  
    from 4kTable 4k  
    cross join (select @str:='SMITH,WARD,KING,TOM') vars 
    on CHAR_LENGTH(@str) 
        -CHAR_LENGTH(REPLACE(@str, ',', ''))>=4k.id-1 
) d 
on d.name=e.ename;

结果:

+-------------+-------+-------+-----------+------+------------+---------+--------+--------+
| rtable_name | empno | ename | job       | mgr  | hiredate   | sal     | comm   | deptno |
+-------------+-------+-------+-----------+------+------------+---------+--------+--------+
| SMITH       |  7369 | SMITH | CLERK     | 7902 | 1980-12-17 |  800.00 |   NULL |     20 |
| WARD        |  7521 | WARD  | SALESMAN  | 7698 | 1981-02-22 | 1250.00 | 500.00 |     30 |
| KING        |  7839 | KING  | PRESIDENT | NULL | 1981-11-17 | 5000.00 |   NULL |     10 |
| TOM         |  NULL | NULL  | NULL      | NULL | NULL       |    NULL |   NULL |   NULL |
+-------------+-------+-------+-----------+------+------------+---------+--------+--------+
MySQL 2022/1/1 18:49:59 有284人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶