(SQL 子查詢 次數統計)-使用『關聯子查詢』實現查詢A表ID在B表出現的次數[EX:一個人領了幾張門禁卡]
(SQL 子查詢 次數統計)-使用『關聯子查詢』實現查詢A表ID在B表出現的次數[EX:一個人領了幾張門禁卡]
SELECT u.id AS id,u.emp_no AS jobnum,u.security_id AS s_id,u.name AS name, d.name AS dname,u.attribute AS attribute,u.birthday AS birthday,(SELECT COUNT(*) FROM card_for_user_car WHERE card_for_user_car.user_id=u.id) AS card_count
FROM user AS u
LEFT JOIN department_detail AS d_d ON ((u.id=d_d.user_id) AND ((d_d.car_id IS NULL) OR (d_d.car_id <1)))
LEFT JOIN department AS d ON (d_d.dep_id=d.id)
ORDER BY u.id;