请帮助优化我的视图,以便连接两个表,并对整个单词列表进行部分匹配。
在附带的屏幕截图中,不应列出ProductID 12和36,因为ProductName中的整个单词不是=“混合”而是SillMix和MixPastora。
SELECT dbo.Products.ProductID, dbo.Products.ProductName, dbo.NAMES.FullName
FROM Products
INNER JOIN dbo.NAMES ON dbo.Products.ProductName LIKE '%' + dbo.NAMES.FullName + '%'
答案 0 :(得分:0)
那么你需要将FullName的值作为一个整体进行比较吗? 尝试更改ON条件,如下所示:
public class PhoneCustomStateListener extends PhoneStateListener {
public int signalSupport = 0;
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
signalSupport = signalStrength.getGsmSignalStrength();
Log.d(getClass().getCanonicalName(), "------ gsm signal --> " + signalSupport);
if (signalSupport > 30) {
Log.d(getClass().getCanonicalName(), "Signal GSM : Good");
} else if (signalSupport > 20 && signalSupport < 30) {
Log.d(getClass().getCanonicalName(), "Signal GSM : Avarage");
} else if (signalSupport < 20 && signalSupport > 3) {
Log.d(getClass().getCanonicalName(), "Signal GSM : Weak");
} else if (signalSupport < 3) {
Log.d(getClass().getCanonicalName(), "Signal GSM : Very weak");
}
}
}
要
dbo.Products.ProductName LIKE '%' + dbo.NAMES.FullName + '%'
答案 1 :(得分:0)
这是一种可以做到这一点的方法。我正在使用字符串拆分器,如果您有大量数据,可能会产生严重的负面性能影响。这个分离器虽然非常快。
这是拆分器。
create FUNCTION [dbo].[DelimitedSplit8K]
--===== Define I/O parameters
(@pString VARCHAR(8000), @pDelimiter CHAR(1))
RETURNS TABLE WITH SCHEMABINDING AS
RETURN
--===== "Inline" CTE Driven "Tally Table" produces values from 0 up to 10,000...
-- enough to cover VARCHAR(8000)
WITH E1(N) AS (
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
), --10E+1 or 10 rows
E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
cteTally(N) AS (--==== This provides the "zero base" and limits the number of rows right up front
-- for both a performance gain and prevention of accidental "overruns"
SELECT 0 UNION ALL
SELECT TOP (DATALENGTH(ISNULL(@pString,1))) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
),
cteStart(N1) AS (--==== This returns N+1 (starting position of each "element" just once for each delimiter)
SELECT t.N+1
FROM cteTally t
WHERE (SUBSTRING(@pString,t.N,1) = @pDelimiter OR t.N = 0)
)
--===== Do the actual split. The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found.
SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY s.N1),
Item = SUBSTRING(@pString,s.N1,ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1,8000))
FROM cteStart s
;
这个分离器是由Jeff Moden创建的,并由社区在sql server central完善。你可以在这里阅读完整的文章。 http://www.sqlservercentral.com/articles/Tally+Table/72993/
分割器还有其他几种选择。关于替代品的最佳文章之一可以在这里找到。 https://sqlperformance.com/2012/07/t-sql-queries/split-strings
现在我们已经介绍了拆分器,我们可以设置一些数据,看看这个是如何工作的。
declare @Products table(ProductID int, ProductName varchar(100))
declare @Names table(FullName varchar(100))
insert @Products (ProductID, ProductName) values
(52, 'Filo Mix')
, (36, 'Inagd SillMix')
, (2, 'Mix Chang')
, (12, 'Queso Manchego La MixPastora')
insert @Names
select 'Mix'
SELECT p.ProductID
, p.ProductName
, n.FullName
FROM @Products p
INNER JOIN @Names n ON p.ProductName LIKE '%' + n.FullName + '%'
cross apply dbo.DelimitedSplit8K(p.ProductName, ' ') x
where x.Item = n.FullName