select * from TBL_THINGS a, ( select thing_code from TBL_THINGS group by thing_code having count(*) > 1 ) b where a.thing_code = b.thing_code thing_code相同而thing_name不同,怎么讲?
select * from TBL_THINGS a where exists(select 1 from TBL_THINGS where thing_code=a.thing_code group by thing_code having count(1)>1) minus select * from (select * from TBL_THINGS where exists(select 1 from TBL_THINGS where thing_code=a.thing_code group by thing_code having count(1)>1)) group by thing_name hvaing count(1)>1
select * from TBL_THINGS where thing_code in ( select a.thing_code from TBL_THINGS a, ( select thing_code from TBL_THINGS group by thing_code having count(*) > 1 ) b where a.thing_code = b.thing_code group by a.thing_code having count(distinct thing_name) > 1 )
select a.* from TBL_THINGS a,TBL_THINGS b where a.thing_code=b.thing_code and a.thing_name<>b.thing_name;
就是用我的SQL语句 select * from TBL_THINGS where thing_code in ( select a.thing_code from TBL_THINGS a, ( select thing_code from TBL_THINGS group by thing_code having count(*) > 1 ) b where a.thing_code = b.thing_code group by a.thing_code having count(distinct thing_name) > 1 )
还有,在sql语句中,使用了in语法,效率很低
我又另想出了一种方法,可能效率要好一些,不过有些thing_name是要通过some_name显示的。select a.thing_code,a.thing_name,b.some_name from TBL_THINGS a,(select thing_code,max(thing_name) some_name group by thing_code having count(*)>1) where a.thing_code=b.thing_code and a.thing_name <> b.some_name;
我的做法是建立一个函数,判断thing_code一样,而thing_name不一样的函数,如下:create or replace function get_two_code(codes varchar2,names varchar2) return integer as lv_count integer; begin select count(*) into lv_count from tbl_things where thing_code=codes and thing_name<>names; return(lv_count); end; / --查询所要的结果 select distinct thing_code, thing_name from tbl_things where get_two_code(thing_code, thing_name)>1 order by thing_code不知道这种效率会不会很低
Lastdrop(空杯) :能否再对你这种方法改进一下,显示我想要的结果。select a.thing_code,a.thing_name,b.some_name from TBL_THINGS a,(select thing_code,max(thing_name) some_name group by thing_code having count(*)>1) where a.thing_code=b.thing_code and a.thing_name <> b.some_name;我的结果是: THING_CODE THING_NAME ---------- ---------- 00111 罐头 00111 饺子 00111 味精
select * from yourtable where f1 in (select b.f1 from (select f1 from yourtable group by f1 having count(*)>1 and max(f2)<>min(f2) ) b) order by f1
看到 spanx(民工) 的SQL得到启发,我的SQL改为select a.thing_code,a.thing_name from TBL_THINGS a,(select thing_code group by thing_code having (count(*)>1 and max(thing_name)<>min(thing_name)) b where a.thing_code=b.thing_code ;应该是效率比较高的了,用视图其实还是查表,而且对索引的利用还可能有问题。
的记录也找出来我要的是thing_code相同而thing_name不同的记录
where a.thing_code = b.thing_code thing_code相同而thing_name不同,怎么讲?
minus
select * from (select * from TBL_THINGS where exists(select 1 from TBL_THINGS where thing_code=a.thing_code group by thing_code having count(1)>1)) group by thing_name hvaing count(1)>1
thing_code thing_name
------------- --------------
00111 罐头
00112 食盐
00113 香蕉
00111 饺子
00112 食盐
00114 西瓜
.......其中00111有两条记录,但名称不一样,要查找出来的
其中00112有两条记录,但名称一样,不要查找出来的即thing_code重复而thing_name不同的,要查找出来
( select a.thing_code from TBL_THINGS a, ( select thing_code from TBL_THINGS group by thing_code having count(*) > 1 ) b
where a.thing_code = b.thing_code
group by a.thing_code
having count(distinct thing_name) > 1
)
"C:\DBS\NWSales.mdb", acReport, "NW Sales for April", _
"Corporate Sales for April"
下一个示例将 ODBC 数据库 (ODBC Database) 的“Authors”表链接到当前数据库中:DoCmd.TransferDatabase acLink, "ODBC Database", _
"ODBC;DSN=DataSource1;UID=User2;PWD=www;LANGUAGE=us_english;" _
& "DATABASE=pubs", acTable, "Authors", "dboAuthors"
TransferDatabase 方法 TransferDatabase 方法执行 Visual Basic 中的 TransferDatabase 操作。有关该操作如何工作的详细内容,请参阅该操作的主题。语法DoCmd.TransferDatabase [transfertype], databasetype, databasename[, objecttype], source, destination[, structureonly][, saveloginid]TransferDatabase 方法具有下列参数:参数 说明 transfertype 下列固有常量之一: acExportacImport(默认值)acLink 如果该参数空缺,将假设为默认常量 (acImport)。 注意 acLink 传输类型不支持 Microsoft Access 项目 (.adp)。 databasetype 字符串表达式,表示一类可用来导入、导出或链接数据的数据库的名称。 Microsoft Access(默认值)Jet 2.xJet 3.xdBase IIIdBbase IVdBase 5Paradox 3.xParadox 4.xParadox 5.xParadox 7.xODBC 数据库 在“宏”窗口中的 TransferDatabase 宏的“数据库类型”操作参数列表中可以查看数据库类型。 databasename 字符串表达式,包含要导入、导出或链接的表的带有数据库路径的完整名称。 objecttype 下列固有常量之一: acTable(默认值)acQueryacFormacReportacMacroacModuleacDataAccessPageacServerViewacDiagramacStoredProcedure 代表要导入、导出或链接的表的对象类型。如果要在两个 Microsoft Access 数据库之间导入或导出表,可以指定一个对象,但不要指定 acTable。如果要将 Microsoft Access 选择查询的结果导出为另一个数据库类型,请指定 acTable 参数。 如果该参数空缺,将假设为默认常量 (acTable)。 注意 显示在“自动列表成员”列表中的 acDefault 常量对于该参数是无效的。必须选择上述常量列表中的常量。 source 字符串表达式,代表要导入、导出或链接的表的对象名称。 destination 字符串表达式,代表在目标数据库中要导入、导出或链接的表的对象名称。 structureonly 该参数为 True (-1) 时只导入或导出一个数据库的表的结构,为 False (0) 将导入或导出表的结构以及其中的数据。 如果该参数空缺,将假设为默认常量 (False)。 saveloginid 该参数为 True 时将保存数据库链接表的连接字符串中的 ODBC 数据库的登录标识 (ID) 和密码。保存之后,就不需要在每一次打开表时进行登录。当不要保存登录名和密码时,可将该参数设置为 False。 如果该参数空缺,将设为默认值 (False)。 该参数只能在 Visual Basic 中使用。 说明语法中的可选参数可以空缺,但是必须包含参数的逗号。如果有一个或多个位于末端的参数空缺,在指定的最后一个参数后面不需使用逗号。ODBC 数据库的管理者可以使 saveloginid 参数所提供的功能失效,以要求所有的用户在每一次连接到 ODBC 数据库时,输入登录标识和密码。注意 还可以使用 ActiveX 数据对象 (ADO) 来创建链接。方法是,使用 Recordset 对象的 ActiveConnection 属性。
from (select distinct thing_code||thing_name, thing_code,thing_name from table)
如下记录值:
thing_code thing_name
------------- --------------
00111 罐头
00112 食盐
00113 香蕉
00111 饺子
00112 食盐
00114 西瓜我要的结果是
00111 罐头
00111 饺子其他的记录都不查找出来,虽然00112有重复,但对应的名称是一样的,所以不查找出来
select * from TBL_THINGS where thing_code in
( select a.thing_code from TBL_THINGS a, ( select thing_code from TBL_THINGS group by thing_code having count(*) > 1 ) b
where a.thing_code = b.thing_code
group by a.thing_code
having count(distinct thing_name) > 1
)
from TBL_THINGS a,(select thing_code,max(thing_name) some_name group by thing_code having count(*)>1)
where a.thing_code=b.thing_code and a.thing_name <> b.some_name;
---------- ---------- ----------
00111 罐头 味精
00111 饺子 味精和我要的结果不一样,要的结果如下:THING_CODE THING_NAME
---------- ----------
00111 罐头
00111 饺子
00111 味精
return integer
as
lv_count integer;
begin
select count(*) into lv_count from tbl_things where thing_code=codes and thing_name<>names;
return(lv_count);
end;
/ --查询所要的结果
select distinct thing_code, thing_name from tbl_things where get_two_code(thing_code, thing_name)>1
order by thing_code不知道这种效率会不会很低
不知道你找出这些thing_code,thing_name做什么用?其实那个用some_name的SQL虽然显示起来有点不够直观,但是该结果集能够满足大部分的操作要求。
from TBL_THINGS a,(select thing_code,max(thing_name) some_name group by thing_code having count(*)>1)
where a.thing_code=b.thing_code and a.thing_name <> b.some_name;我的结果是:
THING_CODE THING_NAME
---------- ----------
00111 罐头
00111 饺子
00111 味精
group by f1 having count(*)>1 and max(f2)<>min(f2)
) b)
order by f1
多级嵌套查询,效率N低。我现在的做法是:
1、先建一个thing_code重复的视图v_code_2
2、再剔除v_code_2中,thing_name相同的记录,建视图v_code_name_2
3、利用v_code_name_2与tbl_things查出所要的结果不知道以上的执行效率是否“最佳”?
from TBL_THINGS a,(select thing_code group by thing_code having (count(*)>1 and max(thing_name)<>min(thing_name)) b
where a.thing_code=b.thing_code ;应该是效率比较高的了,用视图其实还是查表,而且对索引的利用还可能有问题。