sql数据追踪 ( 积分: 70 )

  • 主题发起人 主题发起人 ozhy1
  • 开始时间 开始时间
O

ozhy1

Unregistered / Unconfirmed
GUEST, unregistred user!
我想在表table中实现这样的功能
表:table
字段:a (varchar50) b(varchar50)
1 2
2 3
3 4
4 5
7 8
....................................
提出要查询的内容‘3’
查询结果(即在字段a和b中把与‘3’相关的所有所有历史记录都找到)
表:table
字段:a b
1 2
2 3
3 4
4 5
请各位指点!!!
 
select * from table1
where a||b like (%3%)
 
a||b
是什么意思啊
语句通不过啊???
 
不太懂 4 5这条记录怎么会被找出来的?
你要列举的东西表达清楚啊。是A=3 OR B=3 OR A+B=3 OR A-B=3 OR B-A=3?还是
A=3 OR B=3? 还有是包括3还是等于3?
不过按上面两位的意思是A=3 OR B=3
那么语句为select * from table where a like '%3%' or b like '%3%'
 

通过字段b=3找到相应的字段a=2
通过字段a=2找到相应的字段b=1
通过字段b=1找不到相应的字段a 则第一分支终止
通过字段a=3找到相应的字段b=4
通过字段b=4找到相应的字段a=5
通过字段a=5找不到相应的字段b 则第二分支终止
查询结果:两个分支并级
字段:a b
1 2
2 3
3 4
4 5
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGrids, DB, ADODB;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
ADOQuery2: TADOQuery;
DataSource1: TDataSource;
DataSource2: TDataSource;
DBGrid1: TDBGrid;
DBGrid2: TDBGrid;
Button1: TButton;
Edit1: TEdit;
Memo1: TMemo;
Button2: TButton;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
procedure pdm(Memo: TMemo;edit: TEdit);
function pds(Memo: TMemo):string;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i,j,js:integer;
str:string;
begin
js:=0;
ADOQuery2.Active:=false;
ADOQuery2.SQL.Text:='select * from a where a=''3'' or b=''3'' ';
ADOQuery2.Active:=true;
while js<>ADOQuery2.RecordCount do
begin
//00
js:=ADOQuery2.RecordCount;
Memo1.Clear;
if ADOQuery2.RecordCount>=1 then
begin
//0
ADOQuery2.First;
while not ADOQuery2.Eof do
begin
edit1.Text:=trim(ADOQuery2.fieldbyname('a').AsString);
pdm(Memo1,edit1);
edit1.Text:=trim(ADOQuery2.fieldbyname('b').AsString);
pdm(Memo1,edit1);
ADOQuery2.Next;
end;
end;
//0
str:=pds(Memo1);
str:='select * from a where a in ('+str+') or b in ('+str+')' ;
edit2.Text:=str;
ADOQuery2.Active:=false;
ADOQuery2.SQL.Text:=str;
ADOQuery2.Active:=true;
end;
//00

end;

procedure TForm1.pdm(Memo: TMemo;
edit: TEdit);
var
i,j,t:integer;
begin
j:=memo.Lines.Count;
t:=1;
for i:=0 to j-1 do
begin
if trim(edit.Text)=trim(memo.Lines.Strings)
then
t:=0;
end;
if t=1 then
memo.Lines.Add(trim(edit.Text))
end;

procedure TForm1.Button2Click(Sender: TObject);
begin

edit2.Text:=pds(Memo1);
end;

function TForm1.pds(Memo: TMemo):string;
var
i,j:integer;
s,str,yin:string;
begin
s:='';
yin:='''';
j:=memo1.Lines.Count;
for i:=0 to j-1 do
begin
if i=memo1.Lines.Count-1 then
s:=s+yin+trim(memo1.Lines.Strings)+yin
else
s:=s+yin+trim(memo1.Lines.Strings)+yin+','
end;
str:=s;
pds:=str;
end;

end.
 
我想用单纯的SQL语句该如何解决呢???
 
select * from (select a from 表 union all select b from 表) where a=3
 
不行还是运行不了!!
 
select * from a,b where a=3 or b=3 group by a,b;
 
接受答案了.
 
后退
顶部