If id is null after left join in mysql, the join condition disappears.

Asked 2 years ago, Updated 2 years ago, 45 views

We have tables A, B, and C, and we would like to find A related to a particular C.

SELECT* 
FROM A A
  INNER JOIN B
    ON b.id = a.b_id
  LEFT JOIN CC
    ON c.b_id=b.id AND c.type='hogehoge'
WHERE c.id IS NULL

At this time, other types other than c.type='hogehoge' are also extracted.

mysql

2022-09-30 20:10

1 Answers

If you only want to extract records with type='hogehoge', use c.type='hogehoge' as an extraction condition, not a merge condition.

SELECT* 
FROM A A
  INNER JOIN B
    ON b.id = a.b_id
  LEFT JOIN CC
    ON c.b_id=b.id
WHERE c.type = 'hogehoge'


2022-09-30 20:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.