Avatar
0
tvd12 Enlightened
tvd12 Enlightened
Sự khác biệt giữa Join Where và Join And
Cho 2 bảng thế này: <p> </p> <pre> CREATE TABLE `facebook` ( `name` varchar(45) NOT NULL, `city` varchar(45) DEFAULT NULL, PRIMARY KEY (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; CREATE TABLE `linkedin` ( `name` varchar(45) NOT NULL, `city` varchar(45) DEFAULT NULL, PRIMARY KEY (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; </pre> <p> </p> <p> Với dữ liệu thế này: </p> <p> </p> <p> Bảng facebook: </p> <img src="https://stackask.com/wp-content/uploads/2022/12/Screenshot-2022-12-04-at-18.47.29.png" alt="" class="alignnone size-full wp-image-8797" /> <p> </p> <p> Bảng linkedin: </p> <img src="https://stackask.com/wp-content/uploads/2022/12/Screenshot-2022-12-04-at-18.48.23.png" alt="" class="alignnone size-full wp-image-8798" /> <p> </p> <p> Thì câu truy vấn join where này: </p> <p> </p> <pre> SELECT * FROM facebook LEFT JOIN linkedin ON facebook.name = linkedin.name WHERE facebook.city = 'SF'; </pre> <p> </p> <p> Lại cho kết quả là: </p> <p> </p> <img src="https://stackask.com/wp-content/uploads/2022/12/Screenshot-2022-12-04-at-18.49.32.png" alt="" class="alignnone size-full wp-image-8799" /> <p> </p> <p> Trong khi sử dụng join and: </p> <p> </p> <pre> SELECT * FROM facebook LEFT JOIN linkedin ON facebook.name = linkedin.name AND facebook.city = 'SF'; </pre> <p> </p> <p> Lại cho kết quả là: </p> <p> </p> <img src="https://stackask.com/wp-content/uploads/2022/12/Screenshot-2022-12-04-at-18.50.31.png" alt="" class="alignnone size-full wp-image-8800" /> <p> </p> <p> Vì sao vậy? </p>
Answer