1

假设我有下表:

--------------------
|ID|Yes|No|Filename|
|01|15 |10|1.php   |
|02|12 |5 |2.php   |
|03|6  |1 |3.php   |
--------------------

我想要做的是在 sql select 语句中应用 Yes/Yes+No 并回显最高值的文件名。

Yes 和 No 会不时更新,所以创建另一个列来处理方程式会更好,还是可以在 sql 语句中使用方程式?选择语句会是什么样子?

谢谢

4

2 回答 2

6

对于MySQL,

SELECT  Filename, 
        CAST((Yes / (Yes + No)) AS DECIMAL(10,5)) rating
FROM    table1
WHERE   CAST((Yes / (Yes + No)) AS DECIMAL(10,5)) =
        (
            SELECT MAX(rating)
            FROM
            (
                SELECT CAST((Yes / (Yes + No)) AS DECIMAL(10,5)) rating
                FROM table1
            ) x
        )

SQLFiddle 演示

为了SQL Server

SELECT   TOP 1 WITH TIES Filename, 
         (Yes / (Yes + No)) rating
FROM     table1
ORDER BY rating DESC

SQLFiddle 演示

于 2012-10-25T03:26:55.287 回答
2

您可以使用这种方法:

SELECT TOP 1 sf.Filename
   , (sf.Yes / (sf.Yes + sf.No)) AS RatioValue
FROM dbo.storeFile AS sf
ORDER BY (sf.Yes / (sf.Yes + sf.No)) DESC
于 2012-10-25T03:24:31.030 回答