2015-11-30 160 views
0

如何在一些條件下得到下一個mysql的結果?mysql查詢得到下一個結果

+-----+---------------+-------------------------+ 
| id | update_type | message    | 
+-----+-----------------------------------------+ 
| 1 | 10   | One tow three   | 
| 2 | 20   | No error in this  | 
| 3 | 0   | My testing message  | 
| 4 | 0   | php module test   | 
| 5 | 30   | hello world    | 
| 6 | 0   | team spirit    | 
| 7 | 40   | puzzle game    | 
| 8 | 50   | social game    | 
| 9 | 0   | stackoverflow   | 
|10 | 0   | stackexchange   | 
+-----+---------------+-------------------------+ 

在上表中我怎樣才能得到update_type=20和直到update_type變化30所有消息。 (上表是靜態的,值爲0,可以擴展到多個下一行)

回答

0

您可以使用子查詢來達到此目的。假設每個值發生一次:

select t.* 
from table t 
where t.id >= (select t2.id from t t2 where t2.update_type = 20) and 
     t.id <= (select t2.id from t t2 where t2.update_type = 30); 

如果值出現一次以上,那麼該查詢就會產生錯誤(子查詢預計僅返回一個值)。在這種情況下你想要做什麼不明確,但子查詢中的min()max()可以提供幫助。

0

如果我確實得到了你的問題,你正在尋找SQL解決方案。如果這是重點,那麼你可以在where子句中做出條件。即

SELECT * FROM tablename WHERE columnname BETWEEN 20 AND 30;

你必須與你的表的名稱,以取代tablename。和columnname與表中的相應列。然後你可以在聲明中輸入WHERE。你的情況是BETWEEN