我有一个简单的场景,但没有让它工作。我需要根据两个值为Mysql Database中指定的列'费用'提取一个值。
如果我指定'from'和'to'列之间的数量,我想得到'费用'的值 示例:如果amount为95,则行'fee'的值应为0.99。
我尝试使用之前的传统方法,但现在我想要MYSQL查询方法。请帮忙。
if ($amount < 100) {
$fee = '0.99';
}
else if ($amount > 100 && $amount < 500) {
$fee = '1.99';
}
else if ($amount < 500 && $amount < 1000) {
$fee = '2.99';
}
else if ($amount < 1000 && $amount < 10000) {
$fee = '4.99';
}
else if ($amount > 10000) {
$fee = '9.99';
}
示例PHP代码:
<?
include "dbconnect.php";
$post_amount = $_POST["amount"];
$sql = "SELECT fee FROM fees WHERE from < '$post_amount' AND to > '$post_amount' LIMIT 1";
$result = mysql_query($sql);
while($row = mysql_fetch_array( $result )) {
$fee = $row["fee"];
}
echo "Posted Amount is $post_amount and Fee is $fee";
?>
答案 0 :(得分:1)
如果我收到你的问题,你需要SQL查询。
您可以使用&gt;和&lt;体征:
"SELECT fee FROM tablename WHERE from < '$amount' AND to > '$amount' LIMIT 1"
或者您可以使用BETWEEN(How to search between columns in mysql):
"SELECT fee FROM tablename where '$amount' BETWEEN from AND to LIMIT 1"
还有两件事:
答案 1 :(得分:1)
MySQL有一个名为BETWEEN
的强大运算符:
"SELECT `fee` FROM `table` WHERE ".floatval($amount)." BETWEEN `from` AND `to`"
答案 2 :(得分:-1)
你刚才遇到问题> and <
所有其他的东西都没问题,你不需要其他函数来检查数据库表。
我已经用它检查了它:
$amount = 250;
if ($amount < 100) {
$fee = '0.99';
} else if ($amount > 100 && $amount < 500) {
$fee = '1.99';
} else if ($amount > 500 && $amount < 1000) {
$fee = '2.99';
} else if ($amount > 1000 && $amount < 10000) {
$fee = '4.99';
} else if ($amount > 10000) {
$fee = '9.99';
}
echo $fee;
它的工作正常。