基于PHP strstr或LIKE的SQL命令 [英] SQL command based on PHP strstr or LIKE
问题描述
请有人提供一些帮助.我有一个标准的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屋!