douliang6563 2015-04-14 09:01
浏览 56
已采纳

如何在SELECT php mysql中子字符串WHERE

I have a column kd_barang2 as an id in my table. The values is like this: 010000101.

I want to select the first 7 number (0100001) and then show it.

The script to select is like this:

$queryalat1="select * from tb_master
  where kd_barang2 = '".$data1['kd_barang2']."'
  && jenis_barang='alat habis pakai'
  Group by merk_barang";

and then I tried this kind of code

$queryalat1="select * from tb_master
  where SUBSTRING(kd_barang2,0,7) = '".substr($data1['kd_barang2'],0,7)."'
  && jenis_barang='alat habis pakai'
  Group by merk_barang";

but it failed, because it didn't show any data in my table.

  • 写回答

3条回答 默认 最新

  • dsbezji539113152 2015-04-14 09:17
    关注

    You're using MySQL's SUBSTRING function with the wrong start value. In PHP substr() will start with zero, but in MySQL it starts with 1. So you need to use SUBSTRING(kd_barang2, 1, 7)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch