如果更大,则选择“是”,如果更小

时间:2018-05-04 07:50:09

标签: mysql

简单查询:我希望在Yes字段大于SUM字段时显示yield的列/字段,而在另一方面时包含No

Mysql说我的查询附近有错误(我的整个查询)并没有指定:

select *, IF(CONVERT(float,SUM) > CONVERT(float,yield),'Yes','No') from active_samples_w_seq where id > 100 and id < 200

2 个答案:

答案 0 :(得分:1)

MySQL总是至少会产生错误代码。

您在CONVERT中的参数是错误的,首先指定字段,然后指定类型。

$startingDate = mysqli_real_escape_string($con, $_POST['fm_startingdate']);
$endingDate = mysqli_real_escape_string($con, $_POST['fm_endingdate']);

$foo = timeTotal($startingDate, $endingDate);
function timeTotal($dT1, $dT2){
    $d1 = new DateTime($dT1);
    $d1->sub(new DateInterval("P1D"));
    $d2 = new DateTime($dT2);
    $diff = $d1->diff($d2);
    echo $diff->days;
}

echo $foo // 53

if(mysqli_num_rows($result) == 0){
    $sql = "UPDATE terms SET
            name = '$termNameTrimmed',
            startingdate = '$startingDate',
            endingdate = '$endingDate',
            total = '$foo' // <-- this is not updating as expected
            WHERE term_id = '$termId'";

    if(mysqli_query($con, $sql)){
        $_SESSION['success'] = "Updated Successfully!";
        header("Location: ../../term_months.php".$URLId);
    }
    else {
        echo "Records are NOT Added. Please try again<br />";
        echo mysqli_error ($con);
    }
}
else{
    $_SESSION['exists'] = "<strong>".$termNameTrimmed."</strong> name is already exists!";
    header ("Location: ../../term_month_edit.php".$URLId);
}

https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html#function_convert

答案 1 :(得分:1)

正如@DanFromGermany所说,顺序是CONVERT(expr,type),但是对于浮点值,你应该使用DECIMAL而不是FLOAT:

select *, IF(CONVERT(SUM,decimal) > CONVERT(yield,decimal),'Yes','No') 
from active_samples_w_seq 
where id > 100 and id < 200