php pdo查询范围选择值“Above”

时间:2017-01-09 08:14:56

标签: php mysql select pdo

$stmt = $dbh->prepare("SELECT * FROM salary_table WHERE "
. "(cast(min_compensation as decimal(10, 6)) <= :name AND :name <= cast(max_compensation as decimal(10, 6)) OR cast(min_compensation as decimal(10, 6)) <= :name AND LOWER(max_compensation) = 'above' OR :name <= cast(max_compensation as decimal(10, 6)) AND LOWER(min_compensation) = 'below') AND date_deleted IS NULL"); //AND date_deleted IS NULL
$stmt->bindValue(":name", $name, PDO::PARAM_STR);

数据库记录:

Min : 1000000
Max : Above

搜索参数:

“上方”

此方案应查询具有上述最大值的记录。但是我得到的值最低,因为当Floatval()应用名称时,它变为0所以我所做的就是:

$name = (floatval($_POST['name']) > 0) ? floatval($_POST['name']) : $_POST['name'];

$stmt = $dbh->prepare("SELECT * FROM salary_table WHERE "
. "( (LOWER(max_compensation) = :name OR LOWER(min_compensation) = :name) OR (cast(min_compensation as decimal(10, 6)) <= :name AND :name <= cast(max_compensation as decimal(10, 6)))) AND date_deleted IS NULL"); //AND date_deleted IS NULL
$stmt->bindValue(":name", $name, PDO::PARAM_STR);

FYI

当我回显$name时,它输出:

Above

但我仍然记录最小值为0的记录。

image

上图是我数据库中的记录。当我使用Above 进行搜索时,我应该获得第二条记录而不是第一条记录

  

如何处理此范围字符串比较。

0 个答案:

没有答案