一个字符串‘3;6;7;15;35;99;’
如何查询得到
xx
--
3
6
7
15
35
99
如何查询得到
xx
--
3
6
7
15
35
99
解决方案 »
- mysql where查询中文问题
- Linux下mysql中:如何把character_set_system编码修改为gbk
- 又来求助sql了。
- 数据库总题.答出的分你自己说要多少.
- [求助]c mysql-5.0.22连接数据库的问题
- 初次上路,请帮我解决mysql中的一个小小问题!
- Mysql 求原代码分析???
- <好消息也是坏消息>PHP & MySQL
- mysqldump -X -uroot -ppassword test testA > /opt/text.xml 这样的xml如何进行数据恢复
- 4个表的复杂联合查询,求助!
- cidr的insert问题
- postgresql v8.3.1 所有的postgres进程均结束了。why?
[/align]
LEN(REPLACE('3;6;7;15;35;99',';',''))取得有多少个;
1、用循环执行
ff=SUBSTRING_INDEX('3;6;7;15;35;99',';', i)取値,
2、
REPLACE('3;6;7;15;35;99',ff,'')刷新字符串,重新执行执行第一步思路如此,用要一个存储过程来完成
#建表测试代码
create table test1
(`strA` varchar(20) NOT NULL);
create table test2
(`strB` varchar(20) NOT NULL);
insert into test1 values('2;5;7;13;55;108;266;');
insert into test2 values('2;7;13;108;');
#表中的strA,strB长度是不限的,元素间以;分隔,strB中的元素一定在strA中有。
目标
XX FLAG
--------
2 1
5 0
7 1
13 1
55 0
108 1
266 0
请大家帮忙呀!
2。是否是只能用SQL语句实现?
[align=center]==== 思想重于技巧 ====
[/align]
如果可以,我可以帮你写个store procedure, 不过你的mySQL version >5
2。是否是只能用SQL语句实现?
比较复杂,但亦可实现说明你的要求,省得我做两种方案
[align=center]==== 思想重于技巧 ====
[/align]
很感谢
[/align]
select substring_index(@str,';',1);
select substring(substring_index(@str,';',2),instr(substring_index(@str,';',2),';')+1);...
今天要开会,如果没人帮你写的话,明天帮你写一下[align=center]==== 思想重于技巧 ====
[/align]
role (rid, ...)
permission ( rid, perid,...
userRole ( uid, rid)[align=center]==== 思想重于技巧 ====
[/align]
split_part(‘3;6;7;15;35;99;', ';', 2) = 6;
...
[/align]
其实还有个表字段包含所有ID,
问题重新描叙:
一个表A,有字段ID,包含所有数字。
一个表B,有字符串IDGROUPS‘3;6;7;15;35;99;’
如何查询得到
xx
--
3
6
7
15
35
99#测试代码
DROP TABLE IF EXISTS `testa`;
CREATE TABLE `testa` (
`id` int(11) NOT NULL default '0'
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
INSERT INTO `testa` (`id`) VALUES (1);
INSERT INTO `testa` (`id`) VALUES (2);
INSERT INTO `testa` (`id`) VALUES (3);
INSERT INTO `testa` (`id`) VALUES (4);
INSERT INTO `testa` (`id`) VALUES (5);
INSERT INTO `testa` (`id`) VALUES (6);
INSERT INTO `testa` (`id`) VALUES (7);
INSERT INTO `testa` (`id`) VALUES (8);
INSERT INTO `testa` (`id`) VALUES (9);
INSERT INTO `testa` (`id`) VALUES (10);
INSERT INTO `testa` (`id`) VALUES (11);
DROP TABLE IF EXISTS `testb`;
CREATE TABLE `testb` (
`id` int(11) default NULL,
`IdGroups` varchar(100) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
INSERT INTO `testb` (`id`,`IdGroups`) VALUES (1,'5;4;7;11;9;');
#执行SQL
select a.id as xx FROM testa a where (select FIND_IN_SET(a.id,REPLACE(b.idGroups,';',',')) from testb b where b.id='1');#结果
xx
--
4
5
7
9
11 结帖/