首页 \ 问答 \ Rownum处于连接状态(Rownum in the join condition)

Rownum处于连接状态(Rownum in the join condition)

最近我修复了一些错误:在连接条件下有rownum。

这样的事情:在t1.id = t2.id和rownum <2上左连接t1。 所以它应该只返回一行,而不管“左连接”。

当我进一步研究这个时,我意识到我不明白Oracle如何在“左连接”条件下评估rownum。 我们来创建两个样表:主和细节。

create table MASTER
(
  ID   NUMBER not null,
  NAME VARCHAR2(100)
)
;
alter table MASTER
  add constraint PK_MASTER primary key (ID);

prompt Creating DETAIL...
create table DETAIL
(
  ID            NUMBER not null,
  REF_MASTER_ID NUMBER,
  NAME          VARCHAR2(100)
)
;
alter table DETAIL
  add constraint PK_DETAIL primary key (ID);
alter table DETAIL
  add constraint FK_DETAIL_MASTER foreign key (REF_MASTER_ID)
  references MASTER (ID);

prompt Disabling foreign key constraints for DETAIL...
alter table DETAIL disable constraint FK_DETAIL_MASTER;
prompt Loading MASTER...
insert into MASTER (ID, NAME)
values (1, 'First');
insert into MASTER (ID, NAME)
values (2, 'Second');
commit;
prompt 2 records loaded
prompt Loading DETAIL...
insert into DETAIL (ID, REF_MASTER_ID, NAME)
values (1, 1, 'REF_FIRST1');
insert into DETAIL (ID, REF_MASTER_ID, NAME)
values (2, 1, 'REF_FIRST2');
insert into DETAIL (ID, REF_MASTER_ID, NAME)
values (3, 1, 'REF_FIRST3');
commit;
prompt 3 records loaded
prompt Enabling foreign key constraints for DETAIL...
alter table DETAIL enable constraint FK_DETAIL_MASTER;
set feedback on
set define on
prompt Done.

然后我们有这个查询:

select * from master t
left join detail d on d.ref_master_id=t.id

结果集是可预测的:我们拥有主表中的所有行以及匹配此条件的详细表中的3行d.ref_master_id = t.id。

结果集

然后我将“rownum = 1”添加到连接条件中,结果相同

select * from master t
left join detail d on d.ref_master_id=t.id and rownum=1

最有趣的是我设置“rownum <-666”并再次获得相同的结果!

select * from master t
left join detail d on d.ref_master_id=t.id and rownum<-666.

由于结果集,我们可以说这个条件在详细信息表中被评估为3行的“真”。 但是,如果我使用“内部联接”,一切都应该如此。

select * from master t
join detail d on d.ref_master_id=t.id and rownum<-666.

这个查询不返回任何行,因为我无法想象rownum会少于-666 :-)

而且,如果我使用oracle语法进行外连接,使用“(+)”一切都很顺利。

select * from master m ,detail t
 where m.id=t.ref_master_id(+) and rownum<-666.

此查询不会返回任何行。

任何人都可以告诉我,我和外连接和rownum误解了什么?


Recently I fixed the some bug: there was rownum in the join condition.

Something like this: left join t1 on t1.id=t2.id and rownum<2. So it was supposed to return only one row regardless of the “left join”.

When I looked further into this, I realized that I don’t understand how Oracle evaluates rownum in the "left join" condition. Let’s create two sampe tables: master and detail.

create table MASTER
(
  ID   NUMBER not null,
  NAME VARCHAR2(100)
)
;
alter table MASTER
  add constraint PK_MASTER primary key (ID);

prompt Creating DETAIL...
create table DETAIL
(
  ID            NUMBER not null,
  REF_MASTER_ID NUMBER,
  NAME          VARCHAR2(100)
)
;
alter table DETAIL
  add constraint PK_DETAIL primary key (ID);
alter table DETAIL
  add constraint FK_DETAIL_MASTER foreign key (REF_MASTER_ID)
  references MASTER (ID);

prompt Disabling foreign key constraints for DETAIL...
alter table DETAIL disable constraint FK_DETAIL_MASTER;
prompt Loading MASTER...
insert into MASTER (ID, NAME)
values (1, 'First');
insert into MASTER (ID, NAME)
values (2, 'Second');
commit;
prompt 2 records loaded
prompt Loading DETAIL...
insert into DETAIL (ID, REF_MASTER_ID, NAME)
values (1, 1, 'REF_FIRST1');
insert into DETAIL (ID, REF_MASTER_ID, NAME)
values (2, 1, 'REF_FIRST2');
insert into DETAIL (ID, REF_MASTER_ID, NAME)
values (3, 1, 'REF_FIRST3');
commit;
prompt 3 records loaded
prompt Enabling foreign key constraints for DETAIL...
alter table DETAIL enable constraint FK_DETAIL_MASTER;
set feedback on
set define on
prompt Done.

Then we have this query :

select * from master t
left join detail d on d.ref_master_id=t.id

The result set is predictable: we have all the rows from the master table and 3 rows from the detail table that matched this condition d.ref_master_id=t.id.

Result Set

Then I added “rownum=1” to the join condition and the result was the same

select * from master t
left join detail d on d.ref_master_id=t.id and rownum=1

The most interesting thing is that I set “rownum<-666” and got the same result again!

select * from master t
left join detail d on d.ref_master_id=t.id and rownum<-666.

Due to the result set we can say that this condition was evaluated as “True” for 3 rows in the detail table. But if I use “inner join” everything goes as supposed to be.

select * from master t
join detail d on d.ref_master_id=t.id and rownum<-666.

This query doesn’t return any row,because I can't imagine rownum to be less then -666 :-)

Moreover, if I use oracle syntax for outer join, using “(+)” everything goes well too.

select * from master m ,detail t
 where m.id=t.ref_master_id(+) and rownum<-666.

This query doesn’t return any row too.

Can anyone tell me, what I misunderstand with outer join and rownum?


原文:https://stackoverflow.com/questions/6603968
更新时间:2022-05-12 09:05

最满意答案

您可以简单地使用[ NSString : AnyObject ]类型的Swift字典,它自动桥接到NSDictionaryCFDictionary 。 请注意,您也不需要CFStringRef

let font = CTFontCreateWithName("Courier", 25.0, nil)
let attributes : [ NSString : AnyObject ] = [ kCTFontAttributeName : font ]
let attrString = CFAttributedStringCreate(nil, "Hello", attributes)

或者,

let attrString = NSAttributedString(string: "Hello", attributes: attributes)

因为NSAttributedString是使用CFAttributedString进行免费桥接的。


只是为了完整性,以下是如何使用CFDictionaryCreate()

let font = CTFontCreateWithName("Courier", 25.0, nil)
var keys = [ unsafeAddressOf(kCTFontAttributeName) ]
var values = [ unsafeAddressOf(font) ]
var keyCallbacks = kCFTypeDictionaryKeyCallBacks
var valueCallbacks = kCFTypeDictionaryValueCallBacks
let attributes = CFDictionaryCreate(nil, &keys, &values, 1, &keyCallbacks, &valueCallbacks)
let attrString = CFAttributedStringCreate(nil, "Hello", attributes)

You can simply use a Swift dictionary of type [ NSString : AnyObject ], which is automatically bridged to NSDictionary or CFDictionary. Note that you don't need CFStringRef either.

let font = CTFontCreateWithName("Courier", 25.0, nil)
let attributes : [ NSString : AnyObject ] = [ kCTFontAttributeName : font ]
let attrString = CFAttributedStringCreate(nil, "Hello", attributes)

Alternatively,

let attrString = NSAttributedString(string: "Hello", attributes: attributes)

because NSAttributedString is toll-free bridged with CFAttributedString.


Just for the sake of completeness, here is how you could use CFDictionaryCreate():

let font = CTFontCreateWithName("Courier", 25.0, nil)
var keys = [ unsafeAddressOf(kCTFontAttributeName) ]
var values = [ unsafeAddressOf(font) ]
var keyCallbacks = kCFTypeDictionaryKeyCallBacks
var valueCallbacks = kCFTypeDictionaryValueCallBacks
let attributes = CFDictionaryCreate(nil, &keys, &values, 1, &keyCallbacks, &valueCallbacks)
let attrString = CFAttributedStringCreate(nil, "Hello", attributes)

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。