简体   繁体   中英

Mysql Left Outer Join do not include all recs from left table

I have 2 tables:

The left table1 just include ID and Status as text und just 5 Recs The table2 is linked by ID Table1 to an field which calls status in table2.

What I like to do is to sum the numbers of all types of status. My Idea was to using an left outer join an get a list of all status with numbers behind. in Case there no linked recs from table2 to an status it should a NULL appears.

But my SQL statement just work like an ordinary join and bring only equal records. but not the records from table1 with NULL

I'd apreciate if you can have a look to my SQL statement:

SELECT task.tas_status
  ,v_task_status_1.param_str1
FROM v_task_status_1
LEFT OUTER JOIN task ON (v_task_status_1.param_id = task.tas_status)

You could use COUNT() function to count and then use GROUP BY as well

SELECT 
 t2.tas_status,
 COUNT(*) as CountPerStatus 
FROM v_task_status_1 t1
 LEFT OUTER JOIN task t2 
 ON t1.param_id = t2.tas_status
GROUP BY t2.tas_status

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM