2016-03-01 60 views
-1

,所以如果我有這樣的如何獲得發現之前USER_ID

SELECT * FROM PRIORITY ORDER BY priority_number ASC

id | user_id | priority_number 
------------------------------ 
1 | 1 |  4   
2 | 5 |  5   
3 | 2 |  6   
4 | 3 |  7   
5 | 6 |  8   
6 | 8 |  9   
7 | 7 |  10   
8 | 10 |  12   
9 | 9 |  13   
10 | 12 |  14   
11 | 13 |  15   
12 | 11 |  16   

一個數據庫,然後當我搜索查詢的所有結果user_id = 7然後我會拿到第一的user_id

id | user_id | priority_number 
------------------------------ 
1 | 1 |  4   
2 | 5 |  5   
3 | 2 |  6   
4 | 3 |  7   
5 | 6 |  8   
6 | 8 |  9   
7 | 7 |  10   

我到目前爲止試過的東西。

SELECT * FROM PRIORITY WHERE priority_number <= 10 ORDER BY priority_number ASC但我只有user_id給出

+0

所以你想獲得的所有用戶,在'priority_number'下令,有一個'USER_ID '小於或等於7(或其他任何指定爲參數的值)? – quasoft

+0

我想查找找到的所有查詢user_id –

+0

只需使用子查詢和一個'<=',即'SELECT * FROM PRIORITY WHERE priority_number <=(SELECT priority_number FROM PRIORITY WHERE user_id =?) – 2016-03-01 20:24:59

回答

0

在這種情況下,這將是類似的東西: SELECT * FROM PRIORITY WHERE priority_number <= (SELECT priority_number FROM PRIORITY WHERE user_id = 7) ORDER BY priority_number ASC