2013-10-03 32 views
0

我必須選擇使用含有與數字文本列值從表中數據的文本,就像這樣:BETWEEN操作時的範圍與數量

"SELECT Title, Category from BookList where CategoryField BETWEEN '" 
    + blockCodeStart + "' AND '" + blockCodeEnd + "'"; 

blockCodeStart = "A25" 
blockCodeEnd = "A39" 

我明白這一點,可能僅處理任何文字或數字,但不是兩個。任何幫助讚賞。

+0

是否總是 'A' 或一個字符其次是一些數字? –

+0

有一個偉大的職位在這裏:http://stackoverflow.com/questions/9893329/sql-where-criteria-to-find-names-between-a-f你試圖做基於數字或阿爾法之間的?顯然在這種情況下,它將採用字母數字值之間的值。你可以做的其中一件事就是解析這些值,選出數字字符,但這會在一個大的數據集中永遠消失。你期望的結果是什麼? – BClaydon

+0

#保羅,它並不總是A.事實上,文本範圍從A到Z.然而,只有類似的文本伴隨着兩列(例如,你總是得到「A +數字」比較「A +數字」,或「D +數字「與」D +數字「相比;而不是」A +數字「與」B +數字「相比 我剛剛發現,所以看起來我只需要使用SQL在2列中的數字之間進行選擇 感謝大家! #Claydon,我會檢查一下鏈接。謝謝! –

回答

0

foo BETWEEN bar AND baz只是bar <= foo AND foo <= baz的簡便快捷方式。如果你比較蘋果和橙子,你不能指望得到「正常」的結果 - 因爲你比較了字符串,你會得到字符串結果。你不能指望DB能夠發現A25大於A9。這不是人類。

您必須將字符串分解爲各自的組件,然後將其作爲「本機」值進行比較,例如,

('A' == 'A') and (25 == 39) 
+0

同意。我會簡單的刪除文本部分並在數字之間進行選擇 –

0

如果字段類別中的所有值具有相同的格式'LETTER'+'DIGITS',則可以創建比較類別的函數。

在這個例子中,我創建並使用了模式test

drop table if exists test.aaa; 
drop function if exists test.natural_compare(s1 character varying, s2 character varying); 

create or replace function test.natural_compare(s1 character varying, s2 character varying) 
returns integer 
as 
$$ 
declare 
    s1_s character varying; 
    s2_s character varying; 
    s1_n bigint; 
    s2_n bigint; 
begin 
    s1_s = regexp_replace(s1, '^([^[:digit:]]*).*$', '\1'); 
    s2_s = regexp_replace(s2, '^([^[:digit:]]*).*$', '\1'); 
    if s1_s < s2_s then 
     return -1; 
    elsif s1_s > s2_s then 
     return +1; 
    else 
     s1_n = regexp_replace(s1, '^.*?([[:digit:]]*)$', '\1')::bigint; 
     s2_n = regexp_replace(s2, '^.*?([[:digit:]]*)$', '\1')::bigint; 

     if s1_n < s2_n then 
      return -1; 
     elsif s1_n > s2_n then 
      return +1; 
     else 
      return 0; 
     end if; 
    end if; 
end; 
$$ 
    language plpgsql immutable; 

create table test.aaa (
id serial not null primary key, 
categ character varying 
); 

insert into test.aaa (categ) values 
('A1'), 
('A2'), 
('A34'), 
('A35'), 
('A39'), 
('A355'), 
('B1'), 
('B6') 
; 

select * from test.aaa 
where test.natural_compare('A34', categ) <= 0 and test.natural_compare(categ, 'A39') <= 0 
+0

看到我上面的評論感謝您的時間 –

0

這可能是一個很好的起點:

SQL Fiddle

MySQL的32年5月5日架構設置

create table BookList (
id int not null auto_increment, 
Title varchar(10), 
Category varchar(10), 
CategoryField varchar(10), 
    primary key (id) 
); 

insert into BookList (title, Category, CategoryField) values 
('Title1','CAT1','A2'), 
('Title2','CAT2','A7'), 
('Title3','CAT3','A10'), 
('Title4','CAT4','A12'), 
('Title5','CAT5','A25'), 
('Title6','CAT6','A33'), 
('Title7','CAT7','A39'), 
('Title8','CAT8','A50'), 
('Title6','CAT6','B33') 
; 

查詢1

SELECT Title, Category 
from BookList 
where substring(CategoryField,1,1) = 'A' AND 
CONVERT(substring(CategoryField,2), SIGNED) BETWEEN 25 AND 39 

Results

| TITLE | CATEGORY | 
|--------|----------| 
| Title5 |  CAT5 | 
| Title6 |  CAT6 | 
| Title7 |  CAT7 | 
+0

我意識到在每一行中,列都有相似的字母(例如A總是和B一起去B,等等)。所以我會簡單地去掉每一列的字母,然後在剩下的數字之間進行選擇。真棒!謝謝你的時間 –