C# 如何从连接两个具有所有id的表中选择只有一个不同列的多个列';是使用Nhibernate的唯一标识符
以下是我的表格结构:C# 如何从连接两个具有所有id的表中选择只有一个不同列的多个列';是使用Nhibernate的唯一标识符,c#,mysql,sql,nhibernate,C#,Mysql,Sql,Nhibernate,以下是我的表格结构: create table Department( deptid [UNIQUEIDENTIFIER] ROWGUIDCOL DEFAULT NEWSEQUENTIALID() NOT NULL CONSTRAINT Department_P_KEY PRIMARY KEY, departmentname varchar(100) ) create table Employee( empid [UNIQUEIDENTIFIER] ROWGUI
create table Department(
deptid [UNIQUEIDENTIFIER] ROWGUIDCOL DEFAULT NEWSEQUENTIALID() NOT NULL CONSTRAINT Department_P_KEY PRIMARY KEY,
departmentname varchar(100)
)
create table Employee(
empid [UNIQUEIDENTIFIER] ROWGUIDCOL DEFAULT NEWSEQUENTIALID() NOT NULL CONSTRAINT Employee_P_KEY PRIMARY KEY,
name varchar(50),
city varchar(50),
deptid UNIQUEIDENTIFIER NOT NULL,
foreign key(deptid) references Department(deptid) ON DELETE CASCADE ON UPDATE CASCADE
)
我的问题是,当我选择多个列并在一个列上应用DISTINCT
,并且所有主键和外键都是UNIQUEIDENTIFIER
时,时间查询会给出错误消息。我的查询和错误如下:
select distinct(Department.deptid),min(Employee.empid) as empid
from Employee
inner join Department on Department.deptid = Employee.deptid
group by Department.deptid
错误消息是:
Msg 8117,16级,状态1,第2行操作数数据类型uniqueidentifier对于min运算符无效。
但若主键和外键是int,那个么上面的查询将成功执行,因为min函数支持整数,但如何使用uniqueidentifier键选择不同的记录
下面是我的Nhibernate查询:-
EmployeeEntity employeeEntity = new EmployeeEntity();
employeeEntity.DepartmentMaster = new DepartmentEntity();
ProjectionList projectionList = Projections.ProjectionList();
projectionList.Add(Projections.Distinct(Projections.Property<EmployeeEntity>(x => x.DepartmentMaster)).WithAlias(() => employeeEntity.DepartmentMaster));
projectionList.Add(Projections.Property<EmployeeEntity>(x => x.empid).WithAlias(() => employeeEntity.empid));
IList<EmployeeEntity> query = null;
query = NHSession.QueryOver<EmployeeEntity>()
.Select(projectionList)
.TransformUsing(Transformers.AliasToBean<EmployeeEntity>()).List<EmployeeEntity>();
EmployeeEntity EmployeeEntity=新EmployeeEntity();
employeeEntity.DepartmentMaster=新部门实体();
ProjectionList ProjectionList=Projections.ProjectionList();
Add(Projections.Distinct(Projections.Property(x=>x.DepartmentMaster)).with别名(()=>employeeEntity.DepartmentMaster));
Add(Projections.Property(x=>x.empid).WithAlias(()=>employeeEntity.empid));
IList query=null;
query=NHSession.QueryOver()
.选择(投影列表)
.TransformUsing(Transformers.AliasToBean()).List();
如何按照上面的sql查询,首先在sql中执行distinct查询?只需将UniqueIdentifier转换为字符串即可
select distinct(Department.deptid),min(CAST(Employee.empid AS NVARCHAR(36))) as empid
from Employee
inner join Department on Department.deptid = Employee.deptid
group by Department.deptid