0

需要找到 id 的最大值,通过这个值我需要读取其他列的值。但它受另一种列类型的影响。我使用了这个 sql 命令:

"SELECT * FROM Table WHERE id = (SELECT MAX(id) FROM Table WHERE type = 1)"

ID 列是 bigint 类型,类型是 nchar。我也尝试将它与 type = '1' 一起使用,但同样的问题。错误在“id =”部分之后

谢谢您的回复

编辑:

SqlCeCommand com = new SqlCeCommand();
if (LocalType == '1') { com = new SqlCeCommand("SELECT req_id FROM Requisition WHERE id = (SELECT MAX(id) FROM Requisition WHERE type = 1)", con); }
else if (LocalType == '2') { com = new SqlCeCommand("SELECT req_id FROM Requisition WHERE id = (SELECT MAX(b.id) FROM Requisition AS b WHERE b.type <> 1)", con); }

using (com)
{
   SqlCeDataReader reader = com.ExecuteReader();
}
4

3 回答 3

2

最简单的方法是使用top. 如果这是您的真实代码,那么您需要“转义”单词“table”,因为它是保留字:

select top 1 t.*
from [table] t
where type = '1'
order by id desc
于 2013-06-19T20:49:51.163 回答
1

尝试命名表:

SELECT * 
FROM Table AS a 
WHERE id = (SELECT MAX(b.id) FROM Table AS b WHERE b.type = 1)
于 2013-06-19T20:56:22.150 回答
0

google了一下,发现3.5版本的SQLCE支持SELECT TOP表达式,但格式不同。必须用括号写

SELECT TOP(1) * FROM MyTable
于 2013-06-20T08:57:19.207 回答