juva jacob
juva jacob

Reputation: 115

Issue with mysql joins

I have two tables

Meetings:

m_id    ProjectName
1      Test
2      Test2

Meeting_next:
id   fk_m_id   Meetingdate  status
1     1         9-1-2018      0
1     1         10-1-2018      0
1     1         13-1-2018       1

I want to join this two tables when I left join it I will get duplicate value

Expected output

Array
(
    [0] => Array
        (
            [m_id] => 1
            [ProjectName] => test

            [meetingDate] =>13-1-2018
        )
 [1] => Array
        (
            [m_id] => 2
            [ProjectName] => test2

            [meetingDate] =>
        )


)

I tried -

select * from meetings left join meeting_next on meetings.m_id= meeting_next.fk_m_id where meeting_next.status=1 order by m_id desc

myOutput:

Array
(
    [0] => Array
        (
            [m_id] => 1
            [ProjectName] => test

            [meetingDate] =>13-1-2018
        ) )

Bad luck I got only first Project name. I need second too. Please help me. Any help would be appreciated.

Upvotes: 2

Views: 49

Answers (1)

jeroen
jeroen

Reputation: 91792

Your WHERE condition filters the number of rows to only the row of the first project.

If you want to show both projects, even if there are no meetings with status 1, you need to move the condition to the join condition:

select * 
from meetings 
left join meeting_next 
  on meetings.m_id= meeting_next.fk_m_id 
    and meeting_next.status=1 
order by m_id desc

Now you will get all rows from meetings with only the matching entries from meeting_next.

Upvotes: 3

Related Questions