Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CONTAINS with multiple terms over multiple columns

Tags:

sql

contains

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.

like image 548
hwschuur Avatar asked Feb 10 '10 14:02

hwschuur


People also ask

Can group by clause contain multiple columns?

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.

How do you update multiple columns with multiple conditions?

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.

Can order clause be used for multiple columns?

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.

Can primary key be defined on multiple columns?

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).


1 Answers

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.

like image 189
Quassnoi Avatar answered Nov 06 '22 04:11

Quassnoi