In one of my PHP application, I use left join between two MySQL tables using the following query:
SELECT bs.question_code, bs.question_english, count( st.id ) sqid
FROM bs_qbank_question bs
LEFT JOIN bs_qbank_question_study_link st
ON bs.id = st.question_id
The problem is, it selects only those rows from bs_qbank_question
that have at least one row in bs_qbank_question_study_link
table. If there is no data for a particular row of bs_qbank_question
in bs_qbank_question_study_link
then that row is not selected.
But I need to select each row from bs_qbank_question
and also need one column for each row from bs_qbank_question_study_link
as count the total occurrence of that row.
Can anyone tell me what's wrong with my query and how can I replace count( st.id )
as 0 if there is no data for any row?
- Thanks