1

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.

nirmala
  • 39
  • 1
  • 1
  • 10

3 Answers3

3
select Distinct prjname 
 from project_detail where prjname <> '' and PrjId is not null;
Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
Ankesh Kumar
  • 515
  • 2
  • 17
0

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...
Steve
  • 213,761
  • 22
  • 232
  • 286
0
select Distinct PrjId, prjname  from project_detail where prjname <> '' and PrjId is not null;

It can work for you.

Tien Nguyen Ngoc
  • 1,555
  • 1
  • 8
  • 17