首页 > 数据库 > mysql教程 > MySQL (非存储过程)解决易语言难题

MySQL (非存储过程)解决易语言难题

WBOY
发布: 2016-06-07 14:56:56
原创
1454 人浏览过

很多人用不同的东西来解决易语言难题,咱也凑个热闹。如果用存储过程的话,和一般的编程语言也就雷同了,这里就用SQL的思想,在MySQL中实现一次。 MySQL drop table if exists n1;create temporary table n1(num int (1));insert into n1 values(1), (2), (3)

很多人用不同的东西来解决易语言难题,咱也凑个热闹。如果用存储过程的话,和一般的编程语言也就雷同了,这里就用SQL的思想,在MySQL中实现一次。
MySQL
drop table if exists n1;
create temporary table n1(num int (1));
insert into n1 values(1), (2), (3), (4), (5), (6), (7), (8), (9);

drop table if exists n2;
create temporary table n2
select * from n1;

drop table if exists n3;
create temporary table n3
select * from n1;

drop table if exists nums1;
create temporary table nums1
select n1.num * 100 + n2.num * 10 + n3.num as num
from n1 left join n2
on n1.num <> n2.num
left join n3
on n1.num <> n3.num and n2.num <> n3.num;

drop table if exists nums2;
create temporary table nums2
select * from nums1; 

drop table if exists nums3;
create temporary table nums3
select * from nums1;

select *
from nums1 as n1 left join nums2 as n2
on n1.num <> n2.num
left join nums3 as n3
on n1.num <> n3.num and n2.num <> n3.num
where n1.num * 2 = n2.num and n1.num * 3 = n3.num
and n1.num not rlike concat("[", n2.num, "]")
and n1.num not rlike concat("[", n3.num, "]")
and n2.num not rlike concat("[", n3.num, "]");

drop table if exists n1;
drop table if exists n2;
drop table if exists n3;
drop table if exists nums1;
drop table if exists nums2;
drop table if exists nums3;


结果:
mysql> select *
    -> from nums1 as n1 left join nums2 as n2
    -> on n1.num <> n2.num
    -> left join nums3 as n3
    -> on n1.num <> n3.num and n2.num <> n3.num
    -> where n1.num * 2 = n2.num and n1.num * 3 = n3.num
    -> and n1.num not rlike concat("[", n2.num, "]")
    -> and n1.num not rlike concat("[", n3.num, "]")
    -> and n2.num not rlike concat("[", n3.num, "]");
+------+------+------+
| num  | num  | num  |
+------+------+------+
|  192 |  384 |  576 |
|  219 |  438 |  657 |
|  273 |  546 |  819 |
|  327 |  654 |  981 |
+------+------+------+
4 rows in set (0.03 sec)
登录后复制
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板