2011-11-16 55 views
0

我有一個MySQL數據庫表,結構調查是:022(城市代碼) - 589893232(公司數量) - 011(分機號碼)如何讓mysql查詢這樣的情況?

我:

startphoneNum  | endphoneNum  | department 
022589893232011 | 022589893232021 | sales 
022589893232027 | 022589893232029 | Human Resources 
022589893232041 | 022589893232048 | after sales 

的電話號碼由3個部分組成可以使用php爆炸,使格式022-589893232-011到​​的電話號碼,然後如何通過mysql queryphone number得到department name

例如:

如果我把電話號碼022589893232043,它屬於after sales, 但如何使代碼php mysql query?並得到我需要的結果?謝謝。

+0

你已經嘗試查詢?如果是這樣,請在此粘貼。 –

回答

8

使用之間這樣的查詢:

SELECT department FROM table WHERE '022589893232043' BETWEEN startphoneNum AND endphoneNum; 
+0

+1爲簡單起見 –

0
select department from [table] where [phone number] > startphonenum AND [phonenumber] < endphonenum 

替換的實際值[] ...

1

嘗試此查詢:

SELECT department 
FROM table1 
WHERE startphoneNum >= PHP_VARIABLE AND endphoneNum <= PHP_VARIABLE 
1

看一看在BETWEEN運營商:

$query = "SELECT department 
      FROM myTable 
      WHERE '$phoneNbr' BETWEEN startphoneNum AND endphoneNum;"; 
1

試試這個:

SELECT department FROM your_table 
WHERE LEFT(your_num, 12) = LEFT(startphoneNum, 12) 
    AND CONVERT(RIGHT(your_num, 3), UNSIGNED) BETWEEN 
     CONVERT(RIGHT(startphoneNum, 3), UNSIGNED) AND 
     CONVERT(RIGHT(endphoneNum, 3), UNSIGNED) 
0

選擇從部門DP dp.department_name,phonenumber_table PN其中pn.startphonenum> '價值' 和pn.endphonenum <價值觀