duanqiang6501 2013-07-11 15:33
浏览 34
已采纳

MySQL BETWEEN查询从数据库中的文本字段获取数据

I have a query :

$sql = "SELECT * FROM table WHERE  CONCAT(Prod, Serial) BETWEEN '$start' AND '$end'";

Where Prod and Serial are stored in MySQLi database as a string. I need to update details of items between say AA1000 and AA1005. When I run this query it updates details of units between AA1000 and AA1005 and, "as a bonus", all between AA10000 and AA10050. Is there a way to get exact match for the text type fields using BETWEEN query?

Thanks!

P.S. I know that I need update query to update details. I'm using SELECT for testing purposes.

  • 写回答

4条回答 默认 最新

  • douxiong5438 2013-07-11 15:43
    关注

    This query should do it:

    SELECT *
    FROM TABLE
    WHERE Prod = LEFT('$start', 2)
    AND CAST(Serial AS DECIMAL) BETWEEN SUBSTRING('$start', 3) AND SUBSTRING('$end', 3);
    

    You could also split the start and end strings apart in PHP:

    $prod = substr($start, 0, 2);
    $start_code = substr($start, 2);
    $end_code = substr($end, 2);
    
    $sql = "SELECT *
    FROM TABLE
    WHERE Prod = '$prod'
    AND CAST(Serial AS DECIMAL) BETWEEN $start_code AND $end_code";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效