SQL 语句子查询怎么写?

如何写一个在另一个复合条件的查询记录中进行符合条件的查询

select * from tablename where name='asd' and info='INFO' and code like '001*'

这里的and code like '001*' 能不能作为一个子集(因为上面并列的时候它没有生效)
比如 :select * from tablename where code in select * from tablename where name='asd' and info='INFO' and code like '001*'
我知道这样肯定不对,不知如何解决这个问题?

...全文
722 23 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
只有一个表。
我原来的SQL语句是这样的:

LC_Sql := 'select * from tablename where'
+ lc_str + 'and code like '+ '''' +'001'+'*'+ '''';

query1.sql.add(lc_Sql);
...
...

lc_str 是界面操作得来的一个多项查询的条件,也就是比如下面的这个条件:
name='asd' and info='INFO'

现在的问题就是 当 lc_str是 name='asd'的时候,查询结果符合要求,也就是
它既考虑了code like '001*' 也考虑了 name='asd' ;
现在的问题是:lc_str 为两个以上的条件时,查询后的结果并没有执行
code like '001*'的条件,而是从全部的记录中查旬。

我用的是access97 + bde5+delphi5
从表面上看这没有错误,可实际情况就是这样,不知你们这么看待?

我刚刚受到启发,在 lc_Str 两边加了括号,结果正常了,不过我值得怀疑
这种现象,或者说是为什么?

最后,各位高人辛苦了,稍候结账,不管你回答正确与否,这不重要,重要的是
我们要讨论问题,人人有赏!!:))
BobLeeCn 2002-03-14
  • 打赏
  • 举报
回复
若是SQL SERVER,将 Code Like '001*'改为 Code Like '001%'就可以,不用写子查询。
forgot 2002-03-14
  • 打赏
  • 举报
回复
select a.* from tablename a,(Select * from tablename where code like '001*') b where a.name='asd' and a.info='INFO' and a.code=b.code
forgot 2002-03-14
  • 打赏
  • 举报
回复
select a.* from tablename a,(Select * from tablename where code like '001*') b where where a.name='asd' and a.info='INFO' and a.code=b.code
LXJ2001 2002-03-14
  • 打赏
  • 举报
回复
你把问题描述更清楚一些,如表名,字段,要实现什么样的查询。
LXJ2001 2002-03-14
  • 打赏
  • 举报
回复
如果是一张表用不着子查询:
select * from tablename where name='asd' and info='INFO' and code like '001*', 不好吗?
如果两张表,最好也不用IN子查询。
  • 打赏
  • 举报
回复
提示 from 子句错误
  • 打赏
  • 举报
回复
try to on
  • 打赏
  • 举报
回复
事实证明:

select * from parts_list where (amount_one=2 or weight_one>8) and code in (select Code from parts_list
where code like '002*' )


select * from parts_list where (amount_one=2 or weight_one>8) and code like '002*'

两个的生成结果是一样的


select * from parts_list where amount_one=2 or weight_one>8 and code like '002*' )
表达式能通过但结果不正确。

前两种可以采纳,是正确的。

to mysine(宝兰) :谢谢你,可是access不象sqlserver 它不支持 select * from (select * from ...) where 这样的语句,它提示 from子句错误。不过
你的思路是正确的,非常感谢。


kb(天堂游侠)
LXJ2001(lxj)
ling(ling) 你们都正确,是我犯糊涂了,呵呵,不过学了一招:
select * from parts_list where (amount_one=2 or weight_one>8) and code in (select Code from parts_list
where code like '002*' )
这样写法很清楚,结构清晰,虽有些冗余,也是值得采纳,尤其是大型数据库的时候。



szyws 2002-03-14
  • 打赏
  • 举报
回复
select *
from tablename
where code in (select code from tablename where code like '%001%')
and name='asd'
and info='INFO'

不知道你想到达什么效果,是不是如果没有符合code like '001*'它的条件时就显示全部,有则显示?
如果是用视图就可以解决
create view v_tablename
as
select *
from tablename
where name='asd'
and info='INFO'

select *
from tablename t
left join v_tablename v on v.id=t.id and code like '%001%'
mysine 2002-03-14
  • 打赏
  • 举报
回复
再补充一下!!
lc_str中要写明字段所隶属的表,是你的查询集所生成的表,如现在这个Tablename1
mysine 2002-03-14
  • 打赏
  • 举报
回复
LC_Sql :=
'select *
from
( select * from tablename where code like where
code like ''001%'')
tablename1
Where ' + lc_str ;
query1.sql.add(lc_Sql);

如果不是你的lc_str子句组合有问题,这样一定行的,因为以前我这样用过!!
希望你仔细一点,再试一试,一定会好用的。
有一点耐心,你一定会成功的:)

LXJ2001 2002-03-14
  • 打赏
  • 举报
回复
我把语句在ACCESS中试了一下,没有问题的,like的计算优先级比AND高。
把生成的SQL放入MEMO,复制到ACCESS中执行看看。
ling 2002-03-14
  • 打赏
  • 举报
回复
是不是你的lc_str条件中有or? 这样就需要注意加括号了. 如果全是and应该不会有问题.
kb 2002-03-14
  • 打赏
  • 举报
回复
select * from tablename where name='asd' and info='INFO' and code in (select Code from tablename where code like '001*' )
知足常乐 2002-03-13
  • 打赏
  • 举报
回复
上面的都行吧
kplchx 2002-03-13
  • 打赏
  • 举报
回复
select a.* from (select * from table where code like '001*') a
where a.name='asd' and a.info='INFO'
其实结果应该和以上各位是一样的
kplchx 2002-03-13
  • 打赏
  • 举报
回复
select a.* from ( select * from table where code like '001*') a
where a.name='asd' and info='info'
steave 2002-03-13
  • 打赏
  • 举报
回复
select * from tablename where name='asd' and info='INFO' and code in (select code from 表名 where 条件)

iroi 2002-03-13
  • 打赏
  • 举报
回复
select * form 表名 where "字段" in (select * from "表名" )
加载更多回复(3)

5,929

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧