选择最高薪水

时间:2010-08-22 00:59:43

标签: sql mysql sqlite

假设有工资:

name     lowhours  highhours  wage 
 Default  0.0       40.0       100  
 Default  40.0      50.0       150  
 Default  50.0      70.5       154  
 Default  70.5      100.0      200  
 Brian    0.0       40.0       200  
 Brian    40.0      50.0       250  
 Brian    50.0      60.0       275  
 Brian    60.0      70.0       300  
 Brian    70.0      80.0       325  
 Brian    80.0      9999.0     350  
 Chad     0.0       40.0       130  
 Chad     40.0      9999.0     170 

我目前正在使用此代码(AutoIT脚本:

func getCurrentWage($Employee, $Hour_number)
    Dim  $row
    Local $Wage = 0
    Local $Found = "found"
    _SQLite_QuerySingleRow(-1, "SELECT wage FROM wagetable WHERE name LIKE " & _SQLite_Escape($Employee) & " AND " & _SQLite_Escape($Hour_number) & " BETWEEN lowhours AND highhours;", $row)
    if @error then
        _SQLite_QuerySingleRow(-1, "SELECT wage FROM wagetable WHERE name LIKE 'Default' AND " & _SQLite_Escape($Hour_number) & " BETWEEN lowhours AND highhours;", $row)
        $Found = "not found"
    endif

    If ($row[0] == "") Then Msgbox(0,0,"Error getCurrentWage")
    $Wage = $row[0]

    Debug("Wage='" & $Wage & "'  because " &$Employee&" was "& $Found& " -- and Hours Elapsed is " & $Hour_number, true)
    return $Wage
EndFunc

因此,如果Hour_number处于低时或高时之间,则这两个查询是完美的。 我需要某种查询,它基本上会执行以下操作:

SELECT wage from wagetable WHERE name LIKE $Employee AND max(highhours)

然后只重复一次'默认'是找不到员工。

额外:如果只找到1个查询找不到$ Employee,是否可以尝试'默认'?

1 个答案:

答案 0 :(得分:1)

  

如果仅使用1个查询找不到$ Employee,是否可以尝试'默认'?

  SELECT name, wage, highhours
    FROM wagetable
   WHERE name like 'Brian' OR
         name like 'Default'
ORDER BY name,
         highhours desc

此查询在查询Brian时有效,但是对于任何名称,Default应该以特殊字符_-+@开头存储在数据库中,因为数字和字母排在第一位。

另一种方法是为了我们的参数创建另一个列,名为priority0的值Default和{{1}对于任何其他用户。然后你可以简单地做:

1

当然它是相同的解决方案,但它比依赖默认值名称中的特殊字符更好。