標題:

LIKE operator with a SUBQUERY

發問:

Using the LIKE operator with a SUBQUERY!! how do i issue the query that will use the values coming from another table as patterns... something like this...select field1 from table1 where field2 like '%(select pattern1 from table2)%'where:table1 field1 field2 1 ABC 2 BCD ... 顯示更多 Using the LIKE operator with a SUBQUERY!! how do i issue the query that will use the values coming from another table as patterns... something like this... select field1 from table1 where field2 like '%(select pattern1 from table2)%' where: table1 field1 field2 1 ABC 2 BCD 3 DDD 4 BBB table2 pattern1 A C F and so result set will be: field1 1 2 is this possible? and if so, what is the correct SQL ? :(

最佳解答:

免費註冊體驗

 

此文章來自奇摩知識+如有不便請留言告知

Like 之後的,是一個string,以substring作matching,但係你的(select x from y)所回傳是一個list/table,不可能實現。 如果用subquery,能配以IN。 用法是 select xxx from xx where field in (select xx) 以你現時的結構,可能不可以單一句完成。 (方法1)可以用filter作實現。 (方法2)改一 改個結構,例如加一個table3,基本上是重複存放table1的資料,欄位會是field1,pattern,拆開左。即你的例子為 field1,pattern 1,A 1,B 1,C 2,B 2,C 2,D 3,D 4,B 你要的query則是 select distinct field1 from table 3 where table3.pattern in (select pattern1 from table 2) 咁上下 方法一效率低。如果Query只係很久少需求,選這個。但如果entry很多會不合適。 方法二浪費存放位置。但如果entry多,效率會很好。注意係只要在sql server作一個trigger來update table3。變成對programming 方面基本係transparent。唔使理。如果唔用trigger,也可以用programming logic實現,不過有機會甩拖。最好亦有一個house keeping力能可以re-build table 3。 其他方法有,如不要table3,可以在需要時才build,即係變temp table,當然行query的時間長一點,但可以省一點位置。也不須house keeping。視乎資料多寡,和query頻密否。

其他解答:
arrow
arrow

    vbd391t 發表在 痞客邦 留言(0) 人氣()