mysql - Join table if table has results -
i have few tables
- clubs -> contains information on different clubs
- clubs-leagues -> contains information different leagues hosted clubs
- codes -> contains description of short hand codes ive used make data entry little easier
clubs-leagues has field contains id of clubs form association eachother.
i have following statement
select club.*, sport.name sport, code.description statustext, code1.description agegroup, code2.description gender, leagues.name leaguename, leagues.agegroup age, leagues.division, code3.description leaguegender, leagues.season clubs club join sports sport on sport.id = club.sport join codes code on club.status = code.code join codes code1 on code1.code = club.agegroup join codes code2 on code2.code = club.gender join `clubs-leagues` leagues on leagues.clubid = club.id join codes code3 on leagues.gender = code3.code club.id=(:n)
it works if club has league if there no leagues (club-leagues table) returns no results(i want show results club table if there none in clubs-leagues table)
currently have tried moving"join clubs-leagues
leagues on leagues.clubid = club.id " right after declare clubs club , using left join got no resuls
thanks
as indicated in comments, need use left join
clubs leagues clubs regardless of league membership. sql this:
select club.*, sport.name sport, code.description statustext, code1.description agegroup, code2.description gender, leagues.name leaguename, leagues.agegroup age, leagues.division, code3.description leaguegender, leagues.season clubs club left join `clubs-leagues` leagues on leagues.clubid = club.id join sports sport on sport.id = club.sport join codes code on club.status = code.code join codes code1 on code1.code = club.agegroup join codes code2 on code2.code = club.gender join codes code3 on leagues.gender = code3.code club.id=(:n)
note haven't checked rest of sql, it's ensure it's correct.
Comments
Post a Comment