Beginning Databases with Postgre SQL phần 4

Phiên bản trước đó có thể được cài đặt trên Windows, nhưng điều này đòi hỏi một số phần mềm bổ sung để tạo ra một môi trường UNIX-như. Do đó chúng tôi khuyên bạn nên phiên bản hoặc mới hơn cho các hệ thống Windows. Cuối cùng, chúng tôi sẽ chuẩn bị cho các ví dụ trong các chương tiếp theo bằng cách | II II CHAPTER 7 ADVANCED DATA SELECTION 175 Note In the examples in this chapter as with others we start with clean base data in the sample database so readers can dip into chapters as they choose. This does mean that some of the output will be slightly different if you continue to use sample data from a previous chapter. The downloadable code for this book available from the Downloads section of the Apress web site at http provides scripts to make it easy to drop the tables re-create them and repopulate them with clean data if you wish to do so. Try It Out Use Count Suppose we wanted to know how many customers in the customer table live in the town of Bingham. We could simply write a SQL query like this SELECT FROM customer WHERE town Bingham Or for a more efficient version that returns less data we could write a SQL query like this SELECT customer_id FROM customer WHERE town Bingham This works but in a rather indirect way. Suppose the customer table contained many thousands of customers with perhaps over a thousand of them living in Bingham. In that case we would be retrieving a great deal of data that we don t need. The count function solves this for us by allowing us to retrieve just a single row with the count of the number of selected rows in it. We write our SELECT statement as we normally do but instead of selecting real columns we use count like this bpsimple SELECT count FROM customer WHERE town Bingham count 3 1 row bpsimple If we want to count all the customers we can just omit the WHERE clause bpsimple SELECT count FROM customer count 15 1 row bpsimple You can see we get just a single row with the count in it. If you want to check the answer just replace count with customer_id to show the real data. II II 176 CHAPTER 7 ADVANCED DATA SELECTION How It Works The count function allows us to retrieve a count of objects rather than the objects themselves. It is vastly more efficient than getting the data itself because all of the data that we

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã 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.