1.项目中有 检测的项目 但是检测项目中有 套餐, 套餐算是 单个项目的小集合
2.现在数据库设计出来了,但是奈何sql差,求帮忙
- 项目表
CREATE TABLE tx_detection (
id int UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY COMMENT '检测项目的ID',
is_list tinyint unsigned not null default 0 comment '0不是套餐 1是套餐',
pid enum('a','b','c','d','e') not null comment '检测项目的父ID',
cid varchar(32) not null default '' comment '用作子查询id',
title CHAR(22) NOT NULL DEFAULT '' COMMENT '检测项目的名称',
description CHAR(50) NOT NULL DEFAULT '' COMMENT '检测内容描述',
content VARCHAR(10000) COMMENT '检测项目的文章内容',
status TINYINT(1) NOT NULL DEFAULT '1' COMMENT '状态1 正常 0 关闭',
sort int unsigned not null default 0 comment '排序',
index(pid)--enum是否能用到索引? 这还是个问题
) ENGINE=MyISAM CHARACTER SET utf8;
- sql语句
INSERT INTO `tx_detection`(`id`, `is_list`, `pid`, `cid`, `title`, `description`, `content`, `status`, `sort`) VALUES (1, 0, 'b', '', '检测项目1', '描述', '内容1', 1, 0);
INSERT INTO `tx_detection`(`id`, `is_list`, `pid`, `cid`, `title`, `description`, `content`, `status`, `sort`) VALUES (2, 0, 'b', '', '检测项目2', '描述', '内容2', 1, 0);
INSERT INTO `tx_detection`(`id`, `is_list`, `pid`, `cid`, `title`, `description`, `content`, `status`, `sort`) VALUES (3, 0, 'b', '', '检测项目1', '描述', '内容3', 1, 0);
INSERT INTO `tx_detection`(`id`, `is_list`, `pid`, `cid`, `title`, `description`, `content`, `status`, `sort`) VALUES (4, 0, 'd', '', '检测项目4', '描述4', '内容4', 1, 0);
INSERT INTO `tx_detection`(`id`, `is_list`, `pid`, `cid`, `title`, `description`, `content`, `status`, `sort`) VALUES (5, 0, 'a', '', '检测项目5', '描述5', '内容5', 1, 0);
INSERT INTO `tx_detection`(`id`, `is_list`, `pid`, `cid`, `title`, `description`, `content`, `status`, `sort`) VALUES (6, 0, 'a', '', '检测项目6', '描述6', '内容6', 1, 0);
INSERT INTO `tx_detection`(`id`, `is_list`, `pid`, `cid`, `title`, `description`, `content`, `status`, `sort`) VALUES (7, 1, 'b', '1,3', '套餐A', '套餐A描述', '套餐A内容', 1, 0);
- 自己写的语句
SELECT a.* from tx_detection as a,tx_detection as b where a.id in (select cid from tx_detection where tx_detection.id = 7) and a.id = 7; --没出来任何数据
SELECT a.* from tx_detection as a,tx_detection as b where a.id in (1,3) and a.id = 7;--按理说这种没用到查询出来的vachar 结果也是空的
补充 ,彻底蒙圈
SELECT a.* from tx_detection as a where a.id in (1,3);可以查询到
SELECT a.* from tx_detection as a where a.id in (select cid from tx_detection where id = 7);但是只能查询到1 我在想应该是自己vachar字段的这样写法是不对的,但是还一个问题就是最后加上限制where id = 7 后数据又是空的了,头疼。
再次补充,有点头绪了。
SELECT a.* from tx_detection as a,tx_detection as b where a.id in (select cid from tx_detection where id = 7) and b.id = 7;
但是只能查询到一条 可能是因为 vachar字段的原因 因为in换成(1,3)就可以了,那么这个数据结构该怎么改呢,还有就是因为查询a的数据所以a.id = 7了肯定不行,所以第一次补充的时候出错能理解了
补充3 因为我看别人说in('1','3')这样是可以的我自己也是了下确实可以查询到
SELECT a.* from tx_detection as a,tx_detection as b where a.id in ('1','3') and b.id = 7;
那么数据中vachar 改成 '1','3'这种形式呢。
3.自己写的确实不行啊 ,我想的是先插入c子ID的数据 然后自己查询自己,奈何不对 感觉自己的 cid 这个用vachar是个错误的选择。
4.想实现的效果
查询id=7的时候 把1,3也给查询出来。
A(正常结果):

B(错误SQL):

你不明白的地方就在于一个地方,为什么A可以查询到正常结果,而B只查询到一条,
问题就在于select cid from tx_detection where id = 7不等于(1,3),而是等于('1,3')!!!
为什么加上括号,你应该要理解in后面跟的是要一个结果集,例如(1,3)代表的就是两行的记录
而你的('1,3')只是一行的记录
mysql会遍历tx_detection的每一行id,是否在这个结果集中,所以A就把id=1和3的遍历显示出来,而B在遍历的时候会发现没有一个id='1,3'???
其实不然,mysql算是一种弱类型语言,如果遇到int类型和字符串类型比较时,会自动把字符串转换成int类型,然后比较,而此时的字符串'1,3'在mysql内部转换方式大概就是从左往右扫描,扫到逗号时就转换不了,直接返回之前已经读取的值,你可以这样尝试将字符串转换成整型:
同理,上面的'1,3'已经被mysql转换成int 1了,也就返回id=1的那行记录。
不能再写了,有点啰嗦了,想要正确的结果就是用find_in_set函数