如何选择小于提供的哈希值id的记录?这甚至可能吗?我从php获取hashKey值,并基于此,需要选择小于包含该hashKey的行的id的记录。
在pseudoCode中:
@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"
答案 0 :(得分:1)
您可以尝试这样的事情:
select id from
myTable where id < (
select id
from myTable
where hashkey = '@thisHash'
);
答案 1 :(得分: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 演示