Manish Sahu
Manish Sahu

Reputation: 325

How to remove redundant data while displaying in jsp

I am fetching data from three tables. I want to display these data in jsp page. My problem is that after join data are redundant

I dont want to display redundant data.

My tables are

 create table questions(id integer,title varchar(140),body varchar(2000),
 primary key(id));

 create table question_tags(id integer,tag_name varchar(50),question_id integer,
 primary key(id),foreign key(question_id) references questions(id));


create table answers(id integer,answer varchar(2000),question_id integer,
primary key(id),foreign key(question_id) references questions(id));

Query to fetch data

SELECT questions.*,`question_tags`.*, `answers`.* 
FROM `questions`
JOIN  `answers` 
ON `questions`.`id` = `answers`.`question_id`
JOIN `question_tags` 
ON `questions`.`id` = `question_tags`.`question_id`
WHERE `questions`.`id` = '1'

result after fetching data

 ID     TITLE   BODY    TAG_NAME    QUESTION_ID ANSWER
  1     a   hello   java                 1          good
  1     a   hello   java                 1          exellent
  1     a   hello   java                 1          ok
  1     a   hello   mysql                1          good
  1     a   hello   mysql                1          exellent
  1     a   hello   mysql                1          ok
  1         a   hello   jquery               1          good
  1     a   hello   jquery               1          exellent
  1     a   hello   jquery               1          ok

Here I am fatching data using result set and showing in jsp

I want to show following data in my jsp

  question.id|question.title|question.body|tag_names | answers
      1               a           hello       java        good  
                                              mysql       excellent
                                              jquery      ok                               

This was for only few no of rows. Here total o/p rows are 1*3*3=9 If data are increased in tables then duplicate data are increased how to resolve it. This is sql fiddle for above code

Upvotes: 2

Views: 1221

Answers (2)

Manish Sahu
Manish Sahu

Reputation: 325

What I was expecting was this

SELECT  `questions`.`id` AS  `question_id` ,
(
SELECT GROUP_CONCAT(  `question_tags`.`tag_name` SEPARATOR ';') 
FROM  `question_tags` 
WHERE  `question_id` =1
) AS  `Tags` ,
(
SELECT GROUP_CONCAT(  `answers`.`answer` SEPARATOR ';' ) 
FROM  `answers` 
WHERE  `question_id` =1
) AS  `Answers` 
FROM  `questions` 
WHERE  `questions`.`id` =1

Upvotes: 0

asifsid88
asifsid88

Reputation: 4701

Use distinct clause with your select statement to get no duplicate records

SELECT distinct questions.*,`question_tags`.*, `answers`.* 
FROM `questions`
JOIN  `answers` 
ON `questions`.`id` = `answers`.`question_id`
JOIN `question_tags` 
ON `questions`.`id` = `question_tags`.`question_id`
WHERE `questions`.`id` = '1'

Updated

ResultSet rs = st.execute();   //Assuming that you have the resultset

Set<String> id = new LinkedHashSet<String>();
Set<String> body = new LinkedHashSet<String>();
Set<String> tag_name = new LinkedHashSet<String>();
Set<String> answer = new LinkedHashSet<String>();

while(rs.next())
{
    id.add(rs.getString("id"));
    body.add(rs.getString("body"));
    tag_name.add(rs.getString("tag_name"));
    answer.add(rs.getString("answer"));
}

Because of the LinkedHashSet only distinct values will be stored. Now you need to iterate to populate your table

NOTE : This is limited to a particular question ID

Upvotes: 1

Related Questions