nirmala
nirmala

Reputation: 39

Select single value from database(when table have multiple similar values) in c#

I have a table with 3 columns PrjId, prjname and station. Here is my table example:

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

I want to fetch projects from project_detail table into a dropdown. Here is my SQL query

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

The issue is instead of one test1 project all 3 test1 are displayed in dropdown. I know I have to put some condition but I am not understanding how to do it. Pease help.

Upvotes: 1

Views: 16186

Answers (3)

Steve
Steve

Reputation: 216313

You requirements are for the DISTINCT keyword

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

This will give you a list of the unique prjname in your table.

However there is something that seems not quite right in that table.

If you want to describe a relationship between projects and stations where a project is present in many stations and a station could have many projects then a more correct approach could be with three ralated tables, one for the projects and one for the stations and one for the relationship between the projects and the stations

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...

Upvotes: 0

Ankesh Kumar
Ankesh Kumar

Reputation: 525

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

Upvotes: 3

Tien Nguyen Ngoc
Tien Nguyen Ngoc

Reputation: 1555

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

It can work for you.

Upvotes: 0

Related Questions