Oiliúint
Modúl
Combine query results with set operators - Training
Combine query results with set operators using Transact-SQL.
Ní thacaítear leis an mbrabhsálaí seo a thuilleadh.
Uasghrádú go Microsoft Edge chun leas a bhaint as na gnéithe is déanaí, nuashonruithe slándála, agus tacaíocht theicniúil.
Applies to:
SQL Server
In some instances, you might want to apply a number of search conditions to the same data column. For example, you might want to:
Search for several different names in an employee
table or for employees who are in different salary ranges. This type of search requires an OR condition.
Search for a book title that both starts with the word "The" and contains the word "Cook." This type of search requires an AND condition.
Nóta
The information in this topic applies to search conditions in both the WHERE and HAVING clauses of a query. The examples focus on creating WHERE clauses, but the principles apply to both types of search conditions.
To search for alternative values in the same data column, you specify an OR condition. To search for values that meet several conditions, you specify an AND condition.
Using an OR condition enables you to specify several alternative values to search for in a column. This option expands the scope of the search and can return more rows than searching for a single value.
Nod
You can often use the IN operator instead to search for multiple values in the same data column.
In the Criteria Pane, add the column to search.
In the Filter column for the data column you just added, specify the first condition.
In the Or... column for the same data column, specify the second condition.
The Query and View Designer creates a WHERE clause that contains an OR condition such as the following:
SELECT fname, lname
FROM employees
WHERE (salary < 30000) OR (salary > 100000)
Using an AND condition enables you to specify that values in a column must meet two (or more) conditions for the row to be included in the result set. This option narrows the scope of the search and usually returns fewer rows than searching for a single value.
Nod
If you are searching for a range of values, you can use the BETWEEN operator instead of linking two conditions with AND.
In the Criteria pane, add the column to search.
In the Filter column for the data column you just added, specify the first condition.
Add the same data column to the Criteria pane again, placing it in an empty row of the grid.
In the Filter column for the second instance of the data column, specify the second condition.
The Query Designer creates a WHERE clause that contains an AND condition such as the following:
SELECT title_id, title
FROM titles
WHERE (title LIKE '%Cook%') AND
(title LIKE '%Recipe%')
Conventions for Combining Search Conditions in the Criteria Pane (Visual Database Tools)
Specify Search Criteria (Visual Database Tools)
Oiliúint
Modúl
Combine query results with set operators - Training
Combine query results with set operators using Transact-SQL.
Doiciméadúchán
BETWEEN (Transact-SQL) - SQL Server
BETWEEN is a Transact-SQL language element that specifies a range to test.
TOP (Transact-SQL) - SQL Server
Limits the rows returned in a query result set to a specified number of rows or percentage of rows in the SQL Server Database Engine.
SELECT DISTINCT FROM <model > (DMX) - SQL Server
SELECT DISTINCT FROM <model > (DMX)
UNION (Transact-SQL) - SQL Server
Set Operators - UNION (Transact-SQL)