我有一个像下面这样的mysql表;
+----+-------------+-------------+-------------+-------------+
| Sl | Column1 | Column2 | Column3 | Column4 |
+----+-------------+-------------+-------------+-------------+
| 1 | Data1A | Data2A | Data3A | xxxxxx |
+----+-------------+-------------+-------------+-------------+
| 2 | Data1B | Data2B | Data3B | yyyyyy |
+----+-------------+-------------+-------------+-------------+
| 3 | Data1C | Data2C | Data3C | xxxxxx |
+----+-------------+-------------+-------------+-------------+
| 4 | Data1D | Data2D | Data3D | yyyyyy |
+----+-------------+-------------+-------------+-------------+
| 5 | Data1E | Data2E | Data3E | xxxxxx |
+----+-------------+-------------+-------------+-------------+
我有一个查询$query = "SELECT * FROM table WHERE Column1 LIKE'%$keyword%' AND Column4 = ''";
即使我没有为col4提供任何关键字,我也希望显示结果。但我无法避免这一部分,因为我有时会为col4提供一些数据。顺便说一句我想显示数据,即使我没有要检查col4。我怎么能这样做?除非我在查询中提供xxxxxx或yyyyyy,否则我的查询不应该查找col4。
答案 0 :(得分:1)
为什么不在有数据检查col4时更改查询?
if ($needToCheckColumn4) {
$query = "SELECT * FROM table WHERE Column1 LIKE'%$keyword%' AND Column4 = '$col4'";
} else {
$query = "SELECT * FROM table WHERE Column1 LIKE'%$keyword%'";
}
答案 1 :(得分:0)
$query = "SELECT * FROM table WHERE Column1 LIKE'%$keyword%' AND Column4 LIKE '%$keyword2%'";
如果$ keyword2是一个空字符串,它将匹配任何内容。