I'm trying to perform a CONTAINS query with multiple terms over multiple columns, like this:
SELECT ID
FROM Table
WHERE CONTAINS((Data1,Data2,Data3), '"foo" & "bag" & "weee"')
However, this query does not behave like I want it to: I want it to return all records for which all terms appear at least once in at least one of the columns, like this:
SELECT ID
FROM Table
WHERE CONTAINS((Data1,Data2,Data3), '"foo"')
AND CONTAINS((Data1,Data2,Data3), '"bag"')
AND CONTAINS((Data1,Data2,Data3), '"weee"')
While this query returns the correct results, it needs a separate AND-clause for every term. Is there a way to express the same query with a single where-clause like in the upper example? This would be convenient when including the query in a (fixed) function.
The SQL GROUP BY clause is used along with some aggregate functions to group columns that have the same values in different rows. The group by multiple columns technique is used to retrieve grouped column values from one or more tables of the database by considering more than one column as grouping criteria.
To update multiple columns use the SET clause to specify additional columns. Just like with the single columns you specify a column and its new value, then another set of column and values. In this case each column is separated with a column.
ORDER BY clause is used to sort the returned records in an order. By using ORDER BY clause, we can sort the result in ascending or descending order. This clause can be used with multiple columns as well.
The PRIMARY KEY constraint uniquely identifies each record in a table. Primary keys must contain UNIQUE values, and cannot contain NULL values. A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).
SQL Server
once behaved this way, but it was considered a bug and "corrected".
You need to create a FULLTEXT
index on a computed column:
DROP TABLE t_ft
CREATE TABLE t_ft (id INT NOT NULL,
data1 NVARCHAR(MAX) NOT NULL, data2 NVARCHAR(MAX) NOT NULL, data3 NVARCHAR(MAX) NOT NULL,
totaldata AS data1 + ' ' + data2 + ' ' + data3,
CONSTRAINT pk_ft_id PRIMARY KEY (id))
CREATE FULLTEXT INDEX ON t_ft (totaldata LANGUAGE 1033) KEY INDEX pk_ft_id
INSERT
INTO t_ft
VALUES (1, 'foo bar', 'baz', 'text')
INSERT
INTO t_ft
VALUES (2, 'foo bar', 'bax', 'text')
SELECT *
FROM t_ft
WHERE CONTAINS (*, 'foo AND baz')
In MySQL
, on the contrary, the fulltext index searches and matches across all columns and this is a documented behavior.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With