tailieunhanh - Hướng dẫn học Microsoft SQL Server 2008 part 23

Trong khi hai truy vấn trước đó là rất tương tự, trong truy vấn đầu tiên theo thứ tự ưu tiên tự nhiên cho các toán tử Boolean gây ra và được đánh giá trước OR. Các HAY bao gồm các nhà văn hòa trong kết quả. Các truy vấn thứ hai được sử dụng dấu ngoặc đơn để ra lệnh rõ ràng thứ tự của các toán tử Boolean. Các HAY thu Nhà văn hòa và các sản phẩm với một ProductCode của 1018, 1019, hoặc 1020. . | Part II Manipulating Data with Select Result ProductCode ProductName 1019 Grand Daddy 1020 Black Ghost While the two preceding queries are very similar in the first query the natural order of precedence for Boolean operators caused the AND to be evaluated before the OR. The OR included the Air Writers in the results. The second query used parentheses to explicitly dictate the order of the Boolean operators. The OR collected the Air Writers and products with a ProductCode of 1018 1019 or 1020. This list was then anded with products that included the letter g in their names. Only products 1019 and 1020 passed both of those tests. Best Practice When coding complex Boolean or mathematical expressions explicitly stating your intentions with parentheses and detailed comments reduces misunderstandings and errors based on false assumptions. Surprisingly using the WHERE clause in a SELECT statement does not require the use of a FROM clause or any data source reference at all. A SELECT statement without a FROM clause returns a single row that includes any expressions in the select s column list. A WHERE clause on a non-table SELECT statement serves as a restriction to the entire SELECT statement. If the WHERE condition is true the SELECT statement will function as expected SELECT abc AS col WHERE 1 0 Result col abc 1 row s affected If the WHERE condition is false the SELECT statement is still executed but it returns zero rows SELECT abc AS col WHERE 1 0 Result col 0 row s affected 182 Introducing Basic Query Flow 8 Columns Stars Aliases and Expressions The title of this section may read like a bad tabloid headline but in all seriousness it refers to the fact that the SQL SELECT statement will return columns in the order in which they re listed in the SELECT statement. The column may be any expression or any column in the FROM clause. Following the FROM clause and the WHERE clause the next logical step in the query is the list of returned .