在SQLCE中使用MAX函数选择更多列
本文关键字:选择 函数 MAX SQLCE | 更新日期: 2023-09-27 18:19:02
需要找到id的最大值,并通过此值我需要读取其他列的值。但它受到另一列类型的影响。我使用了以下sql命令:
"SELECT * FROM Table WHERE id = (SELECT MAX(id) FROM Table WHERE type = 1)"
ID列为bigint类型,type为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();
}
最简单的方法是使用top
。如果这是您的实际代码,那么您需要"转义"单词"table",因为它是一个保留词:
select top 1 t.*
from [table] t
where type = '1'
order by id desc
尝试命名表:
SELECT *
FROM Table AS a
WHERE id = (SELECT MAX(b.id) FROM Table AS b WHERE b.type = 1)
在google上搜索了一段时间,发现3.5版本的SQLCE支持SELECT TOP表达式,但格式不同。必须用括号
来写SELECT TOP(1) * FROM MyTable