【问题标题】:How to select one value and match with other value from my database如何从我的数据库中选择一个值并与其他值匹配
【发布时间】:2018-09-18 06:48:42
【问题描述】:

我使用 codeigniter 并存储我的数据 比如 123,234

我的表名是schedule,列名是batch

$batch = 123;
$lot =1; 
$dataTest = $this->db->query("select q_set from schedule where lot='$lot' 
and batch='$batch'")->row();

看我下面的图片

我尝试选择一个值并匹配我发送的值。 ##如何匹配两个值##

【问题讨论】:

  • and batch='$batch' 如果 $batch 是 123,就像在您的代码中一样,您将找不到任何东西,因为 batch 在您的数据库中的值为 123,321
  • 那么任何答案对您有帮助吗?不回应是很粗鲁的

标签: php html codeigniter


【解决方案1】:

数据库中batch列的值是'123,321',变量$batch是123,它们不相等所以不能选择。 您必须发送与数据库中相同的数据。

试试

$lot=1;
$batch = 123321;  //or $batch='123,321'  i'm not sure the date type of variable $batch
$dataTest = $this->db->query("select q_set from schedule where lot='$lot' and batch='$batch'")->row();

【讨论】:

  • 当我插入 seclude 时。这次批量插入多条数据。
  • 批处理的数据类型是什么?字符串还是整数?如果是整数,则需要使 $batch 123321 来选择行。
【解决方案2】:

试试这个

$batch = "123,321";
$lot =1; 

    $dataTest = $this->db->query("select q_set from schedule where lot=".$lot." 
    and batch='".$batch."')->row();

或使用

$batch = "123";
$lot =1; 
$dataTest = $this->db->query("select q_set from schedule where lot=".$lot." 
and batch like '%".$batch."')->row();

【讨论】:

  • 我需要不喜欢的实际数据
  • 那就用第一个吧,好像batch是个字符串
  • like 无论如何都不会工作,我认为% 应该在' 里面并且在最后
  • @kerbholz true :)
【解决方案3】:

使用查询生成器。请注意,如果批处理是一个字符串,它应该是 $batch = "123,234" 加上你不能有整数的逗号。

$this->db->select('q_set');
$this->db->where('lot', $lot);
$this->db->where('batch', $batch);
$q = $this->db->get('schedule');
if ($q->num_rows() > 0}
    print_r($q->row());
} else {
    echo 'no rows';
}

【讨论】:

    【解决方案4】:

    您也可以使用 find_in_set。但它主要用于查找逗号分隔值。

    $this->db->select('q_set');
    $this->db->from("schedule");
    $this->db->where("FIND_IN_SET('$lot', lot)");
    $this->db->where("FIND_IN_SET('$batch', batch)");
    

    --------------或----------------

    你可以试试这个..

      $this->db->select('q_set');
       $this->db->from('schedule as S1');
       $this->db->where('S1.lot',$lot);
       $this->db->where('S1.batch',$batch);
       $query = $this->db->get();
       return $query->row();
    

    【讨论】:

      【解决方案5】:

      你可以使用 IN 运算符,

      $result = $this->db->select('*')->from($table);
      $result = $this->db->where('lot =',$lot);
      $result = $this->db->where_in('batch',$batch);
      $result = $this->db->order_by('id','desc');
      $result = $this->db->get();
      

      where_in() 运算符检查值是否存在于指定列中

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-23
        • 1970-01-01
        • 2020-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多