2015-04-22 33 views
0

假設我有一個數字1234567890並且我想要做某種循環,創建一些可用於MySQL查詢的東西。剝下一個數字並每次返回它減去最後一位

例如:

號是1234567890

查詢可用的部分應該是這樣的:

SELECT * FROM table WHERE column IN (1234567890, 123456789, 12345678, 1234567, 123456, 12345, 1234, 123, 12, 1) 

打破我的頭就這一個,所以我希望有人能幫助我。

非常感謝您提前...

+0

歡迎來到Stack Overflow!這個問題在信息上有點短暫。你可以分享你的嘗試,以及你遇到了什麼問題? –

回答

4

這是你想要的嗎?

where '1234567890' like concat(column, '%') and length(column) > 0 
+2

@ spencer7593。 。 。謝謝。 –

0
$number = "1234567890"; // Has to be string 
$array = array(); 
while(strlen($number) > 1) 
{ 
    $array[] = $number; 
    $number = substr($number, 0, strlen($number)-1); 
} 
$strForMySQL = implode(",", $array); // would return "1234567890,123456789,12345678,........1" 

$query = "SELECT * FROM table WHERE column IN (".$strForMySQL.")"; 
0

你可以使用substr保持剝離與循環次數的最後一個字符。如果你從一個整數開始,你需要將它轉換成一個字符串來使其工作。

$x=1234567; 
$x=(string)$x; 

for($i=0; $i<strlen($x); $i++){ 
    //Write SQL here or store them in an array, print...etc. 
    echo substr($x, 0,strlen($x)-$i); 

} 
相關問題