duannuci4008 2018-06-10 14:19
浏览 53
已采纳

PHP / SQL if变量包含的字符串与表列中的x字符相同

I have a variable which contains multiple dates as string. It's a range of dates. When I send this into the database table, I want to check if the column contains one of these dates. This is hard to do because a variable is a string of multiple dates. Every date excists out of 10 characters. So I send a variable as string into a database which contains multiple or a single date and I want to check if there already is one of those dates which excist out of 10 characters, so if 10 characters without a space between are the same.

Example of a column: https://i.stack.imgur.com/iUiIK.png

    $query_overeenkomst = "SELECT * from $stad WHERE beschikbare_data LIKE '%$date%'" or die(mysql_error());
    $result_overeenkomst = mysqli_query($conn, $query_overeenkomst);
    $rows_overeenkomst = mysqli_num_rows($result_overeenkomst);
    if($rows_overeenkomst>=1){
    echo "There is a match!";
    }
    else
    { 
    echo "No date the same.";
    }

If I use this code, the column has to contain the whole string of $data (which contains all the dates), but I want to check if there is only one or more dates that match.

The data variable:

$data .= $date->format("Y-m-d") . PHP_EOL;

Returns for example:

2018-12-19 2018-12-20 2018-12-21 2018-12-22 2018-12-23 2018-12-24 2018-12-25 
  • 写回答

1条回答 默认 最新

  • dongyaoxiu6244 2018-06-10 14:44
    关注

    You can do something like this:

    $dates = str_replace(PHP_EOL, "','", trim($data));
    $query_overeenkomst = "SELECT * from $stad WHERE beschikbare_data IN ('$dates')";
    

    The query variable will have the following content:

    SELECT * from WHERE beschikbare_data IN ('2018-12-19','2018-12-20','2018-12-21','2018-12-22','2018-12-23','2018-12-24','2018-12-25')
    

    Which will select all rows with the listed dates.

    EDIT: After fuguring out that you store your in the database the list of dates in one row the answer will be different.

    First, you should know that it's a terrible database structure design, you should read about many-to-many relationship.

    In any case, this is how you can solve this without changing any structure:

    $dates = explode(PHP_EOL, trim($data));
    $where = "";
    
    foreach ($dates as $key => $date) {
        $where .= ($key > 0) ? " OR " : "";
        $where .= "beschikbare_data LIKE ('%$date%')";
    }
    
    
    $query_overeenkomst = "SELECT * from $stad WHERE $where";
    

    This will result this SQL query:

    SELECT * from table WHERE beschikbare_data LIKE ('%2018-12-19%') OR beschikbare_data LIKE ('%2018-12-20%') OR beschikbare_data LIKE ('%2018-12-21%') OR beschikbare_data LIKE ('%2018-12-22%') OR beschikbare_data LIKE ('%2018-12-23%') OR beschikbare_data LIKE ('%2018-12-24%') OR beschikbare_data LIKE ('%2018-12-25%')
    

    Notice that this is an extremly slow query, and in order to avoid this you must change your data structure design.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。