我有一个如下的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。

最佳答案

当您有要检查col4的数据时,为什么不只是更改查询?

if ($needToCheckColumn4) {
$query = "SELECT * FROM table WHERE Column1 LIKE'%$keyword%' AND Column4 = '$col4'";
} else {
$query = "SELECT * FROM table WHERE Column1 LIKE'%$keyword%'";
}

10-05 18:16