ok this is hard to explain…
i have the following tables:
table : words
table: word_progress
table: word_set
foreign keys:
words.id = word_set.word_id
word_progress.word_id = words.id
word_progress.user_id = users.id
basically a word is in a word_set. word_progress (it keeps score on a certain word) relates to a user_id and word_id.
the problem is sometimes there is not an entry in word_progress for user_id and word_id. but when it IS there, i wanna be able to use word_progress in the WHERE part of the query. but if it is not there I dont. My work around at the moment is before running the statement, i do an “insert IGNORE IF EXISTS into work_progress (word_id,user_id) values (?,?)” to make sure its there
i have this query
select
words.* ,
word_progress.progress from words
left join word_progress on word_progress.word_id = words.id
left join word_set on word_set.id = words.word_set_id
where word_set.id = ? and word_progress.user_id = ?
but… the problem is sometimes there is no word_progress entry for that word
how can i do this?
you’re already left-joining, so when there’s no data available, you’ll just get
nullas value for yourword_progress-fields. just check for that like this:another way would be to add the user-restriction directly to the
join-criteria like this:and drop that criteria from the
where-part.note that, in both cases, you’ll have to handle the
null-cases forprogresslater in your code properly.