2013-09-25 28 views
0

如何選擇小於所提供的散列值的ID的記錄?這甚至有可能嗎?我從php獲取hashKey值,並基於此,需要選擇小於包含該hashKey的行的id的記錄。當提供另一列的值時,選擇小於當前ID的記錄

僞代碼:

@thisHash = '08d448b86a3b2f7d05a721ef18a8f02f'; 
select id from myTable where id < {id belonging to @thisHash}; 

表數據

"id" "country" "hashKey" 
"1"  "US"  "319f64ab2c62ee50cc9571f22e3f167b" 
"2"  "US"  "9a24e339723632c1f8d4ceb82b6aa098" 
"3"  "US"  "021109a71c26e252f75a69905da79d27" 
"4"  "US"  "b5f7718d108c9fbc3ad546832d3632d3" 
"5"  "SE"  "382155ea04502c976fccbab35dcce290" 
"6"  "SE"  "79c1ff6bac2c3c36495ee9c08fc8c211" 
"7"  "US"  "21e2eb330732e6d74fc19e260dd5f26f" 
"8"  "US"  "e6ef5608014f4e9b697a217010c801a6" 
"9"  "SE"  "a53b3d20ada68715b07d09cde2d1e7ba" 
"10" "US"  "08d448b86a3b2f7d05a721ef18a8f02f" 
"11" "US"  "a9ec1a4ce8784249a4ae1c400cc75c8f" 
"12" "SE"  "cedfb21356dd656fe2fd9b2442342b10" 
"13" "US"  "26314a78584de4efcb2b3b4069ba2c43" 
"14" "US"  "b180e756567fb9896e77ee05da9d9b14" 

回答

1

使用JOIN

SELECT t1.id 
    FROM table1 t1 JOIN table1 t2 
    ON t2.hashKey = '08d448b86a3b2f7d05a721ef18a8f02f' 
    AND t1.id < t2.id 

注意:取決於MySQL的版本基於連接的查詢有更好的機會進行適當的優化。

輸出:

 
| ID | 
|----| 
| 1 | 
| 2 | 
| 3 | 
| 4 | 
| 5 | 
| 6 | 
| 7 | 
| 8 | 
| 9 | 

這裏是SQLFiddle演示

1

你可以嘗試這樣的事:

select id from 
myTable where id < (
    select id 
    from myTable 
    where hashkey = '@thisHash' 
    ); 
相關問題