基于PHP strstr或LIKE的SQL命令 [英] SQL command based on PHP strstr or LIKE

查看:107
本文介绍了基于PHP strstr或LIKE的SQL命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请有人提供一些帮助.我有一个标准的SQL命令,如下所示:

Could somebody please offer some assistance. I have a standard SQL command as follows

$SQL = "SELECT * FROM table WHERE Date > :FromDate AND Date < :EndDate";

这可以正常工作,但返回不相关的行.我必须利用php循环并使用if语句遍历它们以过滤出所需的记录.

This works fine but returns rows that are not relevant. I am having to make use of a php loop and go through them to filter out the desired records using an if statement.

$col1 = $col1["colVal"]

if(strstr($col1, "ABC") !== FALSE) {
    // can be used on page
}

sql中是否可以执行此操作,所以我只返回所需的结果?我需要它来检查col值字符串仅包含作为ABC的前几个字母.以下sql可以工作吗?

Is there a way of doing this within the sql so I only return required results? I need it to check that the col value string only contains the first letters as ABC. Would the following sql work?

$string = "ABC";

$SQL = "
SELECT * FROM table 
  WHERE 
    Date > :FromDate 
    AND Date < :EndDate 
    AND col1 LIKE $string
";

推荐答案

您使用strstr的方式与substr相同,其中字符串包含ABC.

You're using strstr in the same manner as substr, where the string contains ABC.

SELECT *
    FROM table
    WHERE Date > :FromDate
    AND Date < :EndDate
    AND col1 LIKE '%$string%';

如果要搜索第一个字符为ABC的字符串,请使用:

If you want to search for a string where the first characters are ABC, use:

SELECT *
    FROM table
    WHERE Date > :FromDate
    AND Date < :EndDate
    AND col1 LIKE '$string%';

但是,

stristr完全是另外一个故事.

stristr, however, would be a different story altogether.

这篇关于基于PHP strstr或LIKE的SQL命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆