现有表A数据如下
字段 A B C
a1 b1 c1
a2 b2 c2
a1 b2 c3
a2 b1 c4
要求:根据利用A,B两个字段用一句SQL实现检索得到对应行的记录例:(a1,b1)(a2,b2)
a1 b1 c1
a2 b2 c2
字段 A B C
a1 b1 c1
a2 b2 c2
a1 b2 c3
a2 b1 c4
要求:根据利用A,B两个字段用一句SQL实现检索得到对应行的记录例:(a1,b1)(a2,b2)
a1 b1 c1
a2 b2 c2
select * from tt a where not exists(select 1 from tt where a.a=a and a.id>id)
Where (A='a1' and b='b1') or (A='a2' and b='b2')
'截至2010-11-16 15:49:17 用户结帖率0.00%当您的问题得到解答后请及时结贴.
http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html
http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html8、如何给分和结贴?
http://community.csdn.net/Help/HelpCenter.htm#结帖
(a1,b1)(a2,b2)这种数据是不定的
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
但现在是两个字段,如果两个都用in的话,好像有点问题
create table test1
(
id1 varchar(10),
name1 varchar(20),
password varchar(20)
)
insert into test1 select '01','a1','b1'
insert into test1 select '02','a2','b2'
insert into test1 select '03','a1','b2'
insert into test1 select '04','a2','b1'
【'a1','b1'】和【'a2','b2'】
得到记录
【'01','a1','b1'】和【'02','a2','b2'】但像【'a1','b1'】和【'a2','b2'】的数据是动态的,可能有时是2组,有时是3组
不能通过外部程序拼接字符串
or
用FIND_IN_SET、INSTR之类的函数
select *
from machine
where concat(datagettime,',',delete_flag) ='20101111025903,1'
结果:
ERROR: function concat(character, unknown, character) does not exist
LINE 3: where concat(datagettime,',',delete_flag) ='20101111025903,1...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.但我通过你这个方案想到了另一个,就是做个临时表,将两个字段拼成一个字段,然后用in
where (name1,password) in (select 'a1','b1' union all select 'a2','b2')
from machine
where
(datagettime,delete_flag) in (('20101111025903','1'),('20101111094800','1'))