2017-06-20 40 views
0

我有一个包含3列PrjId,prjname和station的表。 这里是我的表例如:在c#中选择来自数据库的单个值(当表中有多个相似的值时)

PrjId  prjname  station 
1   test1  s1 
1   test1  s2  
1   test1  s3 

我想获取来自project_detail表项目进入一个下拉。 这里是我的SQL查询

select * from project_detail where prjname <> '' and PrjId is not null; 

的问题,而不是一个test1的项目中的所有3个test1的显示在下拉列表中。我知道我必须提出一些条件,但我不知道如何去做。 Pease帮助。

+0

你尝试过顶部1还是截然不同? –

+1

[如何通过SQL选择唯一记录]可能的重复(https://stackoverflow.com/questions/1641718/how-to-select-unique-records-by-sql) – unicorn2

回答

2
select Distinct prjname 
from project_detail where prjname <> '' and PrjId is not null; 
0

您的要求为DISTINCT关键字

select DISTINCT prjname 
from project_detail 
where prjname <> '' and PrjId is not null; 

这将使你在表中的唯一prjname的列表。

但是在表格中有些东西似乎不太正确。

如果你想描述项目和站点之间的关系,其中一个项目出现在许多站点并且一个站点可能有许多项目,那么更正确的方法可以是三个相互关联的表格,一个用于项目,另一个用于站和一个用于项目和站

Project_Detail 
--------------- 
idproj 
prjname 
....other specific project attributes 

Station_Detail 
---------------- 
idstation 
stname 
....oter specific station attributes 

Project_Station 
---------------- 
idprj 
idstation 
....other specific relationship attributes 
....like for example dateofinstall,active etc... 
0
select Distinct PrjId, prjname from project_detail where prjname <> '' and PrjId is not null; 

它可以为你工作之间的关系。

相关问题