Trying to extract data from multiple SQL tables. I have a main table and a couple of sub-tables. I want to get all the rows from the main table but this creates a duplicate record.
Edit:
Lets say , I want to display all the topics along with its status(ch_status). If two topics marked as inprogress and complete then complete will show. Here is the flow , I have Topics table which contains several topics. Now there is also a table named used_topics_chapters which will contain topic along with chapters. When lets say user start chapter , a row will be inserted into complete table which will contain progress status along with user id.
let say there are 5 rows as follows .
1 Topic1 InProgress 1
2 Topic1 Complete 0
3 Topic2 AnotherStatus 1
4 Topic3 NoStatus 1
5 Topic2 complete 0
Then its should display as follows,
1 Topic1 Complete 0
2 Topic2 Complete 0
3 Topic3 NoStatus 1
Here is a example fiddle with sample data and what I have tried so far:
Create table topics
(
id int,
name varchar(10)
)
insert into topics values(1,'Topic 1'),(2,'Topic 2'),(3,'Topic 3')
--
create table used_topics_chapters(id int,topic_id int,name varchar(10))
insert into used_topics_chapters values(1,1,'Chap1'),(2,3,'Chap 2'),(3,3,'Chap3')
--
create table completed
(
id int,
chp_id int,
ch_status varchar(20),
user_id int
)
insert into completed values(1,3,'complete',100),(2,2,'In-Progress',101)
select t.id,t.name,c.ch_status,
case c.ch_status when 'complete' then 0 else 1 end as can_modify
from topics t
left join used_topics_chapters as utc on t.id=utc.topic_id
left join completed as c on c.chp_id=utc.id
http://sqlfiddle.com/#!18/da7d17/2
On the output You could see that Topic 3 is showing 2 times because status is different .
What I want is , if any of the status is complete then only show that.
Ouput what I am getting ,
+----+---------+-------------+------------+
| id | name | ch_status | can_modify |
+----+---------+-------------+------------+
| 1 | Topic 1 | (null) | 1 |
+----+---------+-------------+------------+
| 2 | Topic 2 | (null) | 1 |
+----+---------+-------------+------------+
| 3 | Topic 3 | In-Progress | 1 |
+----+---------+-------------+------------+
| 3 | Topic 3 | complete | 0 |
+----+---------+-------------+------------+
Output what I am expecting ,
+----+---------+-----------+------------+
| id | name | ch_status | can_modify |
+----+---------+-----------+------------+
| 1 | Topic 1 | (null) | 1 |
+----+---------+-----------+------------+
| 2 | Topic 2 | (null) | 1 |
+----+---------+-----------+------------+
| 3 | Topic 3 | complete | 0 |
+----+---------+-----------+------------+