MySQL - select value or return zero? -


i have 2 tables:

1) students

id,  name,   lastname,  class, ...and more columns (not important)... 1,   peter,  fish,      4a 2,   johnny, rock,      4a 3,   tony,   martin,    4b 4,   david,  blur,      4c     5,   joe,    black,     4b 

2) math_class

student_id,  points     1,           15 1,           20   2,           30     3,           11 3,            5 4,            3 

now, with

"select id, name, lastname, sum(points) students, math_class id = student_id group id" 

i can simple list of students followed sum of points have @ math_class.

what need list contains 2 students have no entry in math_class (either id , name no information after or followed 0).

any ideas how solve this? thanks!

what need left join instead.

select id, name, lastname, sum(coalesce(points))  students left join math_class on id = student_id group id 

note use of coalesce avoid null apearing in resultset.

btw, highly recommend use lower case table , column names.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -