PHP and MySQL Web Development - P49

PHP and MySQL Web Development - P49: PHP and MySQL Web Development teaches the reader to develop dynamic, secure, commercial Web sites. Using the same accessible, popular teaching style of the first edition, this best-selling book has been updated to reflect the rapidly changing landscape of MySQL and PHP. | 212 Chapter 9 Working with Your MySQL Database Finding Rows That Don t Match The other main type ofjoin that you will use in MySQL is the left join. In the previous examples you ll notice that only the rows where there was a match between the tables were included. Sometimes we specifically want the rows where there s no match for example customers who have never placed an order or books that have never been ordered. The easiest way to answer this type of question in MySQL is to use a left join. A left join will match up rows on a specified join condition between two tables. If there s no matching row in the right table a row will be added to the result that contains NULL values in the right columns. Let s look at an example select from customers left join orders on This SQL query uses a left join to join Customers with will notice that the left join uses a slightly different syntax for the join condition in this case the join condition goes in a special ON clause of the SQL statement. The result of this query is ------------ ------------------ --------- customerid name orderid -_ 1 Julie Smith 2 2 Alan Wong 3 3 Michelle Arthur 1 3 Michelle Arthur 4 4 Melissa Jones NULL 5 Michael Archer __ NULL This output shows us that there are no matching orderids for customers Melissa Jones and Michael Archer because the orderids for those customers are NULLs. If we want to see only the customers who haven t ordered anything we can do this by checking for those NULLs in the primary key field of the right table in this case orderid as that should not be NULL in any real rows select from customers left join orders using customerid where is null Retrieving Data from the Database 213 The result is ------------- --------------- customerid name ------------- --------------- 4 Melissa Jones 5 Michael Archer ------------- ---------------

Không thể tạo bản xem trước, hãy bấm tải xuống
122    10    1    02-04-2023
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.