I have joined two tables without problems, but in the last union I am having problems, specifically in the valuations
one that is numbering the value of products that do not have a value, for example:
The prueba 1
, has no rating (that is, no user voted for this product) however the value is appearing (1)
to me instead prueba 2
if it has two users who have voted for that product.
So how can I fix this little error, this is my query:
$stmt = $con->prepare("SELECT co.id_course,
au.author,
ca.program_lang,
co.study_program,
co.launch,
co.start_date,
co.release_date,
co.title_course,
co.subtitle_course,
co.price_old,
co.price,
co.url,
AVG(ra.rating) AS avg_rating,
COUNT(*) valuations
FROM tbl_courses co
JOIN tbl_category ca ON co.category = ca.id_category
LEFT JOIN tbl_author au ON co.author = au.id_user
LEFT JOIN tbl_ratings ra ON co.id_course = ra.id_course
WHERE ca.program_lang = ? AND co.active = ?
GROUP BY co.id_course
ORDER BY co.id_course DESC
LIMIT ?");
You have these two values:
The first takes the average of the ratings, the second counts the number of rows... whether they have a rating or not. Try to use
Because count will not add a row if the rating field is
null
.