我在使用LIKE搜索SQL时遇到问题。我希望SQL LIKE搜索所有可以混合的字符。让我给你演示。 数据库用户:
pkg load statistics
% create 1000 samples from a known multivariate normal
Observations = mvnrnd( [0,0], [4, 1; 1 ,2], 1000 );
% Get mean and covariance estimates from sample
SampleMean = mean( Observations, 1 ) % average along rows
SampleCovariance = cov( Observations )
% Get mean and standard deviation in first dimension (i.e. "x-axis")
Mean_firstDimension = SampleMean(1)
StdDev_firstDimension = sqrt( SampleCovariance(1,1) )
% Get gaussian values at malanobis distance of 1, 2, and 3
MVN_value_at_mahalanobis_distance_of_one = mvnpdf( [ Mean_firstDimension + StdDev_firstDimension, 0], SampleMean, SampleCovariance )
MVN_value_at_mahalanobis_distance_of_two = mvnpdf( [ Mean_firstDimension + 2 * StdDev_firstDimension, 0], SampleMean, SampleCovariance )
MVN_value_at_mahalanobis_distance_of_three = mvnpdf( [ Mean_firstDimension + 3 * StdDev_firstDimension, 0], SampleMean, SampleCovariance )
% Define grid:
[GridX, GridY] = ndgrid( -8:0.1:8, -8:0.1:8 );
GridValues = mvnpdf( [GridX(:), GridY(:)], SampleMean, SampleCovariance );
GridValues = reshape( GridValues, size( GridX ) );
% Plot Observations
plot( Observations(:,1), Observations(:,2), 'o', 'markerfacecolor', 'g', 'markeredgecolor', [0,0.5,0], 'linewidth', 1.5 );
hold on;
% Plot contours over grid
contour( GridX, GridY, GridValues, ...
[ MVN_value_at_mahalanobis_distance_of_three, ...
MVN_value_at_mahalanobis_distance_of_two, ...
MVN_value_at_mahalanobis_distance_of_one ...
],
'linewidth', 2
)
hold off;
% Set nice limits and colours for background
axis([-8, +8, -8, +8]); axis equal square;
set(gca, 'color', 'k');
set(gcf, 'color', [0.75, 0.75, 0.75]);
SQL是:
----------------------------------------------
id fname lname phoone email
----------------------------------------------
1 John Smit 51451 john@gmail.com
----------------------------------------------
2 Maria Doul 878988 maria@gmail.com
----------------------------------------------
3 Bob Coul 879333 bob@gmail.com
----------------------------------------------
上面的SQL运行正常。但我也想像这样严谨地搜索:
SELECT * FROM users WHERE CONCAT(fname, lname, phone, email) LIKE
'%Smi%514%'
上面的SQL返回NULL,因为在CONCATENATION中,“ John”在“ Smi”之前。有什么方法可以使SQL查询以两种方式进行搜索。
答案 0 :(得分:1)
每个搜索词都需要一个单独的like
,如下所示:
SELECT *
FROM users
WHERE CONCAT(fname, lname, phone, email) LIKE '%Smi%'
AND CONCAT(fname, lname, phone, email) LIKE '%514%'
AND CONCAT(fname, lname, phone, email) LIKE '%John%'
答案 1 :(得分:0)
也考虑
FULLTEXT(fname, lname, phone, email)
与
一起MATCH(fname, lname, phone, email) AGAINST('+John +Smith +514' IN BOOLEAN MODE)