There is the following database structure

CREATE TABLE `student` ( `id_student` INT(11) NOT NULL AUTO_INCREMENT, `last_name` VARCHAR(100) NOT NULL, `name` VARCHAR(50) NOT NULL DEFAULT '', `middle_name` VARCHAR(100) NOT NULL, PRIMARY KEY (`id_stud`) ) COLLATE='utf8_general_ci' ENGINE=InnoDB AUTO_INCREMENT=5 ; CREATE TABLE `table2` ( `id_student` INT(11) NOT NULL AUTO_INCREMENT, `pole1` INT(11) NULL DEFAULT NULL, `pole2` INT(11) NULL DEFAULT NULL, `pole3` INT(11) NULL DEFAULT NULL, PRIMARY KEY (`id_student`), CONSTRAINT `table2_ibfk_1` FOREIGN KEY (`id_student`) REFERENCES `student` (`id_student`) ) COLLATE='utf8_general_ci' ENGINE=InnoDB ; CREATE TABLE `table3` ( `id_student` INT(11) NOT NULL AUTO_INCREMENT, `pole1` INT(11) NULL DEFAULT NULL, `pole2` INT(11) NULL DEFAULT NULL, `pole3` INT(11) NULL DEFAULT NULL, PRIMARY KEY (`id_student`), CONSTRAINT `table3_ibfk_1` FOREIGN KEY (`id_student`) REFERENCES `student` (`id_student`) ) COLLATE='utf8_general_ci' ENGINE=InnoDB ; 

It is necessary to deduce all students and to make data counting in tables table2, table3. To put in one table 3 fields (pole1, pole2, pole3) and in another, and then among themselves. I made a query that only works together if there are entries in the tables (table1, table2). And I need to display all students even if there are no records in the tables, then output is just 0.

This structure should be:

last_name | name | middle_name | Suma = (pole1 + pole2 + pole3 + pole1 + pole2 + pole3) |

 SELECT st.last_name, st.name, st.middle_name, st.room, (t2.pole1+t2.pole2+t2.pole3) FROM student as st, table2 as t2 WHERE del.id_d = st.id_stud; 

    1 answer 1

    Use an external connection and COALESCE .