150分,询问,如何在access中筛选出两个表中的不同的数据?
1楼: 表1(A)内容为:
type
B1
B2
B3
如何变为
Select B1,B2,B3 From test 形式?
字串5 2楼: select a.*,b.*,c.* from
((select type form table_name where type = B1) AS A,
(select type form table_name where type = B2) AS B,
(select type form table_name where type = B3) AS C) 字串5 3楼: select a.*,b.*,c.* from
(select type form table_name where type = B1) AS A,
(select type form table_name where type = B2) AS B,
(select type form table_name where type = B3) AS C 字串1 4楼: TTSTROLLER: 这样不行,我将你的‘table_name ’ 改为A了,因为TYPE在
A表中,在SQL中调试不过。 字串5 5楼: select B.*,C.*,D.* from
(select type form A where type = B1) AS B,
(select type form A where type = B2) AS C,
(select type form A where type = B3) AS D
字串1
6楼: TTSTROLLER: 我也按你的第二种方法试过,不行,我上面的意思是
表A 中有 TYPE 字段内容为
B1
B2
B3
现在要在TEST 表中查找 B1 B2 B3 的内容。 字串6
7楼: 你把两个表结构发上来看看! 字串7 8楼: 表A 结构: ID(INT) TYPE(CHAR) 1 B1 2 B2 3 B3 . . . . . . 表TEST 结构: ID(INT) B1(DATETIME) B2(DATETIME) B3(DATETIME)...... 1 7:00 8:00 10:00 2 11:00 12:00 13:00 字串7 9楼: 欢迎大家来灌水。字串7
10楼: 仅仅通过SQL不可能实现,
只能先通过一条语句把表A中的TYPE都取出来,然后通过程序构造SELECT语句
很简单的,不必要搞的很复杂。 字串2
11楼: select case when type=‘B1‘ then left(type,100) end as B1,
case when type=‘B2‘ then left(type,100) end as B2,
case when type=‘B3‘ then left(type,100) end as B3
from type in (‘B1‘,‘B2‘,‘B3‘) 字串5
12楼: 还是把TYPE取出来再执行SQL吧,把SQL写的那复杂干什么 字串7 13楼: 看不懂,这样有何意义?字串8
14楼: 请大家帮帮忙。在线等待。字串3
15楼: zuoc的辦法還不能達到你的要求么?不就是將列划排?字串7
16楼: Jelly0228: 怎样写 zuoc的,我调试过结果为: 字段名 B1 B2 B2 记录:b1 null null null b2 null null null b3字串4
17楼: Oh﹐那是還要加上Max(B1),Max(B2)這樣的語句。
SQL Server在線幫助中就有這樣的例子。 字串8 18楼: 路过而已,继续... 字串4 19楼: 我试试,谢谢 字串1 20楼: Jelly0228:你好,我调过还是达不到我想要的效果。
因为,A 表的内容是 B1,B2,B3, 它又相当于TEST表的字段名B1,B2,B3
我调试的结果为
字段名:B1,B2,B3
记录:B1,B2,B3
我想要的效果是:B1 B2 B3
7:00 8:00 10:00
11:00 12:00 13:00
也就相当于执行 SELECT B1, B2, B3 FROM TEST 而B1,B2,B3要用A表中的
记录A.B1,A.B2,A.B3代替。
请指教!
字串7 21楼: 你应该还有一个对应表吧?
B1 值1
B1 值2
。。。
B2 值1
。。。
B3 值1
.。。 字串1 22楼: 引力:
请问如果按你的想法该怎么做?能不能贴一下。 字串5
23楼: 看不懂,
你的DateTime值在哪里?
表A是不是应该为:
ID type typevalue
1 B1 7:00
2 B1 11:00
3 B2 8:00
..........
或者
你的表A,
表B:
ID 对应ID 值 ??? 字串2
24楼: 呵呵,问题是还没搞懂你的意思。 字串3 25楼: datetime不是字段名,而是TEST表中 B1,B2,B3的数据类型为时间型。 字串8
26楼: 我知道了,你的意思是不是根据表A中type字段的值来动态建立test表 字串5 27楼: 我是想根据表A中type字段的值来动态显示test表的内容。 字串2
28楼: 用一句SQL语句我写不出来,
你可以在程序中控制。
我想你可以先查询出表A中type的内容加到列表框里,
偱环加载列表框的内容,中间加逗号然后赋给一字符串变量。
最后执行SQL:
procedure TForm1.Button1Click(Sender: TObject);
begin
//xxx字符串变量
with ADODataSet1 do
begin
close;
CommandText := ‘select ‘+xxx+‘ from test‘;
open;
end;
end; 字串7
29楼: 引力: 谢谢你,我是想将它用一个存储过程写出来,不知有没有好的方法? 字串4
30楼: 用Case
不大明白你所要求的意思,但我觉得你所要得到的结果是要用CASE
举个例子
表ProductPart : ProductID,PartID,Content
productid partid content
-------------------------------
111 01 010101
111 02 020202
111 03 030303
22 01 220101
22 03 220303
建存储过程 如下:
CREATE PROCEDURE [dbo].[xTest] AS
DECLARE @sql varchar(8000)
SET @sql = ‘select ProductID,‘
SELECT @sql = @sql + ‘(case PartID when ‘‘‘ + PartID + ‘‘‘
then Content end) as ‘‘‘
+ PartID + ‘‘‘,‘
FROM (SELECT DISTINCT PartID
FROM ProductPart) AS a
SELECT @sql = LEFT(@sql, len(@sql) - 1)
+ ‘ from ProductPart group by ProductID, PartID, Content‘ EXEC (@sql)
执行结果:
productid 01 02 03
-------------------------
111 010101 null null
111 null 020202 null
111 null null 030303
22 220101 null null
22 null null 220303
现在有个问题就是,不知道怎么把它合在一起
就是变成为:
productid 01 02 03
-------------------------
111 010101 020202 030303
22 220101 null 220303 字串5
31楼: create procedure aaa
as
declare @tmpfield varchar(50)
declare @tmpsql varchar(200)
DECLARE cu1 CURSOR
FOR SELECT distinct type FROM test1
OPEN cu1
FETCH NEXT FROM cu1 into @tmpfield
set @tmpsql=‘‘
WHILE @@FETCH_STATUS = 0
BEGIN
set @tmpsql=@tmpsql+‘,‘+@tmpfield
FETCH NEXT FROM cu1 into @tmpfield
end
set @tmpsql=right(@tmpsql,len(@tmpsql)-1)
CLOSE cu1
DEALLOCATE cu1
if @tmpsql=‘‘ return
else
begin
set @tmpsql=‘select ‘+@tmpsql+‘ from test2‘
exec (@tmpsql)
end 字串8 32楼: 学到东西了,谢谢楼主,谢谢victor_zh
主要用到 服务器游标 DECLARE CURSOR
sp_executesql
执行可以多次重用或动态生成的 Transact-SQL 语句或批处理 字串5 33楼: 问题复杂化了
1、做一个循环把(用c++ bulider 语法)
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add(“SELECT * FROM A“)
ADOQuery1->Open();
String myselect=“ select “
while (!ADOQuery1->Eof)
{
myselect=myselect+ADOQuery1->FieldByName(“type“)->AsString.Trim()
ADOQuery1->Next()
if(!ADOQuery1->Eof)
{ myselect=myselect+“,“;
}
}
2.打开表test 表TEST
ADOQuery1->Close();
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add(myselect)
ADOQuery1->Open(); 字串6 上一篇:一个棘手SQL问题! 下一篇:没有了