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.
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
最满意答案
您可以简单地使用
[ NSString : AnyObject ]
类型的Swift字典,它自动桥接到NSDictionary
或CFDictionary
。 请注意,您也不需要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 toNSDictionary
orCFDictionary
. Note that you don't needCFStringRef
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 withCFAttributedString
.
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)
相关问答
更多-
我想你想说的是这样的: imageData = UnsafeMutablePointer
.alloc(dataLength) I think what you want to say is something like this: imageData = UnsafeMutablePointer .alloc(dataLength) -
可以使用UnsafeMutablePointer初始化UnsafePointers // Any UnsafeMutablePointer with capacity one let ump = UnsafeMutablePointer
.allocate(capacity: 1) // `up` now contains an UnsafePointer initialized with `ump` var up = UnsafePointer(ump) UnsafePointers can b ... -
在Swift 3上: var somePointer = UnsafeMutableRawPointer(bitPattern: 1)! somePointer = UnsafeMutableRawPointer(mutating: &somePointer) print(somePointer) print(somePointer.load(as: UnsafeMutableRawPointer.self)) // Output of both should be the same. 斯威夫特2: 使 ...
-
你可以简单地传递一个整数。 它不使用它,但在这种情况下无关紧要。 sa.display(0); 如果您打算在子类中重载此函数,并且实际将使用此整数,则此方法在实际情况下可能很有用。 You could simply pass an integer to it. It doesn't use it, but that doesn't matter in this context. sa.display(0); Such an approach may be useful in the real situa ...
-
将void作为函数参数传递(Pass Void as Function Parameter)[2022-07-03]
为什么不只是指定一个非模板重载? void* Callback; templatevoid Function(T Parameter) { (function )Callback(Parameter); } void Function() { (function )Callback(); } Why not just specify a non-template overload? void* Callback; templa ... -
什么参数类型void *&mean以及它的用途是什么?(What does the parameter type void*& mean and what's its use?)[2023-12-16]
很难说没有看到它的用处,但你指出参考是一个间接的层面。 如果它是一个指向指针的指针,你会发现它很奇怪吗? 这些都很常见 - 事实上,当你接受命令行参数并获取指向char指针的指针时,他们会处理它们。 另一个例子是如果你使用桶创建哈希映射,并希望能够将指针返回到启动桶的指针。 问题的关键在于有时候你需要多层次的间接。 确实,混合指针和引用可能很古怪,但有充足的理由。 一个常见原因是需要C ++代码与C apis进行交互。 许多POSIX系统调用都需要传入并更改的空指针,因此所讨论的C ++函数可能会充当一个包 ... -
基本上,这是在这里讨论的问题的一个变种: Xcode 8 beta 6:main.swift不会编译 问题是你的CommandLine.unsafeArgv类型和C函数期望的类型之间有阻抗不匹配。 而且,你不能仅仅通过强制从一个可变指针类型到另一个可变指针类型来抛弃这种不匹配。 相反,你必须通过调用bindMemory从一种类型转换到另一种类型。 并要求输入一个Optional
>.Type的错误消息,告诉您要Optional >. ... -
好的,让我们看看你的代码: dataLength = UnsafeMutablePointer
.alloc(1) dataLength.initialize(bytesRead.length) dataLength是一个传入的指针,它是函数调用者给你的缓冲区大小并希望你输入你读取的字节数的地方。 您不需要分配它,它已经被分配。 (与此示例无关,但是:同样在alloc(N)和initialize(N)中,N应该是相同的(它是分配的内存量,然后初始化)) 我认为你想要的(Swift 3使用point ... -
您可以简单地使用[ NSString : AnyObject ]类型的Swift字典,它自动桥接到NSDictionary或CFDictionary 。 请注意,您也不需要CFStringRef 。 let font = CTFontCreateWithName("Courier", 25.0, nil) let attributes : [ NSString : AnyObject ] = [ kCTFontAttributeName : font ] let attrString = CFAttribu ...
-
NSData(bytes:, length:)将UnsafePointer
作为第一个参数,并且您不能在此处传递任意SequenceType 。 您可以传递一个Array ,该Array将作为第一个数组元素的地址传递。 但是不能保证Array元素存储在连续的内存中。 因此: 定义Array扩展而不是Sequence扩展。 使用withUnsafeBufferPointer()方法获取指向连续数组存储的指针。 可能的方法: extension Array where Element : Intege ...