如何使mysql查询像这样的情况?

时间:2011-11-16 08:00:55

标签: php mysql

我有一个mysql数据库表,其结构是:

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

电话号码由3部分组成:022(城市代码) - 589893232(公司号码) - 011(分机号码)

我可以使用php explode,从022-589893232-011022589893232011格式创建一个电话号码,然后如何通过phone number放置mysql query来获取{{1} }}?

例如:

如果我拨打电话号码department name,则该号码属于022589893232043, 但是如何在代码中创建after sales?得到我需要的结果?谢谢。

6 个答案:

答案 0 :(得分:8)

使用BETWEEN这样的查询:

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

答案 1 :(得分:1)

尝试此查询:

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

答案 2 :(得分:1)

查看BETWEEN运算符:

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

答案 3 :(得分: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)

答案 4 :(得分:0)

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

用实际值替换[] ...

答案 5 :(得分:0)

从部门dp,phonenumber_table pn中选择dp.department_name,其中pn.startphonenum&gt; 'values'和pn.endphonenum&lt; “值