搜索几个表中的SQL
-
19-09-2019 - |
题
我想在一次搜索几个表的搜索词。我的查询是:
SELECT item.ItemID
FROM Inventory.Item item
JOIN Inventory.Category catR // each item can be in several categories
ON catR.ItemID = item.ItemID
JOIN Category.Category cat
ON cat.CategoryID = catR.CategoryID
JOIN Inventory.Brand bran
ON bran.BrandID = item.BrandID
WHERE
item.Description LIKE '%' + @term + '%'
OR
item.Description LIKE '%' + @term
OR
item.Description LIKE @term + '%'
OR
item.Description = @term
OR
cat.CategoryName LIKE '%' + @term + '%'
//same pattern as item.Description used to search CategoryName
//...
OR
bran.BrandName LIKE '%' + @term + '%'
//same pattern as item.Description used to search BrandName
//...
但是,并不如预期的结果。我在类别“平开窗”,但约50个项目,当长期==“凯斯门特”只能在具有“凯斯门特”项目在他们的item.Description将被退回。
我是不是做错了什么?我应该做这更好的办法?
解决方案
这是一个很好的CTE例如:
WITH items AS (
SELECT i.itemid,
i.description,
cat.category_name,
b.brandname
FROM INVENTORY.ITEM i
LEFT JOIN INVENTORY.CATEGORY c ON c.itemid = i.itemid
LEFT JOIN CATEGORY.CATEGORY cat ON cat.CategoryID = c.categoryid
LEFT JOIN INVENTORY.BRAND b ON b.brandid = i.brandid)
SELECT a.itemid
FROM items a
WHERE a.description LIKE '%' + @term + '%'
UNION ALL
SELECT b.itemid
FROM items b
WHERE b.categoryname LIKE '%' + @term + '%'
UNION ALL
SELECT c.itemid
FROM items c
WHERE c.brandname LIKE '%' + @term + '%'
的CTE在SQL Server 2005+支撑。
其他提示
它足够写
item.Description LIKE '%' + @term + '%'
代替
的item.Description LIKE '%' + @term + '%'
OR
item.Description LIKE '%' + @term
OR
item.Description LIKE @term + '%'
OR
item.Description = @term
在概念上我会保持它的简单,然后根据需要对性能,从那里改变。首先,我将创建一个视图,然后执行选择关闭的这一点。
CREATE VIEW vSearchTables
AS
SELECT item.ItemID, 'Item' AS TableName, item.Descripton AS Txt
FROM Inventory.Item item
UNION ALL
SELECT catR.ItemID, 'Category' AS TableName, cat.CategoryName AS Txt
FROM Inventory.Category catR
JOIN Category.Category cat
ON cat.CategoryID = catR.CategoryID
UNION ALL
SELECT item.ItemID, 'Brand' AS TableName, bran.BrandName AS Txt
FROM Inventory.Item item
JOIN Inventory.Brand bran
ON bran.BrandID = item.BrandID
GO
SELECT ItemID
FROM vSearchTables
WHERE Txt LIKE '%'+@term +'%'
GO
如果您有SQL2005和要测试这个概念,可以运行以下命令:
CREATE VIEW vSearchTables
AS
select object_name(o.object_id) Object, o.type, m.definition as Txt
from sys.sql_modules m
join sys.objects o on m.object_id = o.object_id
GO
SELECT *
FROM vSearchTables
WHERE Txt LIKE '%TRIGGER%'
尝试这种情况:
Select i.ItemID
From Inventory.Item i
Join Inventory.Category ic
On ic.ItemID = i.ItemID
Join Category.Category cc
On cat.CategoryID = ic.CategoryID
Join Inventory.Brand ib
On ib.BrandID = i.BrandID
Where CharIndex(@Term,
i.Description + '|' +
ic.Description + '|' +
cc.Description + '|' +
ib.Description) > 0
实现,这将导致所有四个表的全表扫描,所以这将是缓慢的,如果这些表都很大。但考虑到你正在做什么,唯一的选择是执行对数据库的全文索引...
另外,如果是possiblke这些表中的一个不包含匹配的行为年加入条件,你应该让所有的联接外部联接,并在所有的列引用使用IsNull()
...
Select i.ItemID
From Inventory.Item i
Left Join Inventory.Category ic
On ic.ItemID = i.ItemID
Left Join Category.Category cc
On cat.CategoryID = ic.CategoryID
Left Join Inventory.Brand ib
On ib.BrandID = i.BrandID
Where CharIndex(@Term,
i.Description + '|' +
IsNull(ic.Description, '') + '|' +
IsNull(cc.Description, '') + '|' +
IsNull(ib.Description, '')) > 0
不隶属于 StackOverflow