这些天我是一个 MySQL 的人并且有一个我正在处理的 .NET 项目并且无法弄清楚如何根据表中的两个字段动态生成和返回表中的字段(列)表。
Units 表中有两个字段 bRentable 和 bRented,如果 bRentable 和 bRented 为零,我需要返回一个名为 reserved 的字段。
这是我到目前为止的 SQL
/* Units Query */
SELECT Units.UnitID, Units.dcWidth, Units.dcLength, Units.dcPushRate,
Units.dcStdRate, Units.UnitTypeID, UnitTypes.sTypeName, Units.bRentable
FROM Units
INNER JOIN UnitTypes ON Units.UnitTypeID = UnitTypes.UnitTypeID
如有任何帮助,我们将不胜感激。
最佳答案
您可以将以下内容作为另一列添加到您的选择语句中:
(cast case
when (bRentable = 0 and bRented = 0) then 1
else 0
end as bit) as reserved
编辑:根据 OP 评论更新:
SELECT Units.UnitID, Units.dcWidth, Units.dcLength, Units.dcPushRate,
Units.dcStdRate, Units.UnitTypeID, UnitTypes.sTypeName, Units.bRentable,(cast case
when (bRentable = 0 and bRented = 0) then 1
else 0
end as bit) as reserved
FROM Units
INNER JOIN UnitTypes ON Units.UnitTypeID = UnitTypes.UnitTypeID
关于c# - 如何根据其他两个字段的值从 MSSQL 返回一个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12312232/