有人可以提供一些帮助吗?我有一个标准的 SQL 命令如下
$SQL = "SELECT * FROM table WHERE Date > :FromDate AND Date < :EndDate";
这工作正常但返回不相关的行。我必须使用 php 循环并通过它们来使用 if
语句过滤出所需的记录。
$col1 = $col1["colVal"]
if(strstr($col1, "ABC") !== FALSE) {
// can be used on page
}
有没有办法在 sql
中执行此操作,以便我只返回所需的结果?我需要它来检查 col
值字符串是否仅包含第一个字母作为 ABC
。以下 sql
是否有效?
$string = "ABC";
$SQL = "
SELECT * FROM table
WHERE
Date > :FromDate
AND Date < :EndDate
AND col1 LIKE $string
";
最佳答案
您使用 strstr
的方式与 substr
相同,其中字符串包含 ABC
。
SELECT *
FROM table
WHERE Date > :FromDate
AND Date < :EndDate
AND col1 LIKE '%$string%';
如果要搜索第一个字符为 ABC
的字符串,请使用:
SELECT *
FROM table
WHERE Date > :FromDate
AND Date < :EndDate
AND col1 LIKE '$string%';
但是,stristr
将是完全不同的故事。
关于php - 基于 PHP strstr 或 LIKE 的 SQL 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18343862/