如果我要搜索城市地址,例如,#34;奎松市",那就是结果。但是,如果我要搜索城市地址和省份,如奎松市,马尼拉大都会区,那么就没有结果。
此时此处是我的SQL查询。
//TABLES
t1 = street
t3 = province
t4 = city
t5 = village
SELECT t1.*
, t1.name AS propertyname
, t2.name AS typeName
, t3.name AS provName
, t4.name AS cityName
, t5.name AS brgyName
, t6.imgpath_tn AS imgName
FROM proplistings AS t1
LEFT JOIN proptypes AS t2 ON t2.id = t1.property_type_id
LEFT JOIN tbl_province AS t3 ON t3.id = t1.prov_id
LEFT JOIN tbl_city AS t4 ON t4.id = t1.city_id
LEFT JOIN tbl_barangay AS t5 ON t5.id = t1.brgy_id
LEFT JOIN proplistings_images AS t6 ON t6.propid = t1.id
WHERE t1.deleted = 0
AND t1.announceas = '1'
AND t6.defaultimg = 1
AND t6.imgpath != ''
AND t1.status=1
AND CONCAT(t1.street,', t5.name,', ', t4.name,', ', t3.name, ',t1.propcode,', ')
LIKE '%Quezon City, Metro Manila%'
ORDER BY t1.date_added DESC
答案 0 :(得分:0)
如果concat并且任何列包含null,则结果为null。尝试编码为null。
例如
set @a = 'abc';
set @b = null;
select @a,@b,concat(@a,@b),
concat(ifnull(@a,''),ifnull(@b,''));
+------+------+---------------+-------------------------------------+
| @a | @b | concat(@a,@b) | concat(ifnull(@a,''),ifnull(@b,'')) |
+------+------+---------------+-------------------------------------+
| abc | NULL | NULL | abc |
+------+------+---------------+-------------------------------------+
1 row in set (0.00 sec)
答案 1 :(得分:0)
我发现了2个问题。
我看到您按以下顺序连接:... + province + city + ...
因此,如果Quezon City
是城市而Metro Manila
是省,则连接结果如下:Metro Manila, Quezon City
所以你有使用LIKE '%Metro Manila, Quezon City%'
代替LIKE '%Quezon City, Metro Manila%'
。
此外,您必须使用IFNULL
来避免连接空值,因为如果CONCAT
中的任何值为NULL
,则整个结果将为NULL
。
所以你的WHERE
子句应该是这样的:
WHERE t1.deleted = 0
AND t1.announceas = '1'
AND t6.defaultimg = 1
AND t6.imgpath != ''
AND t1.status=1
AND CONCAT(IFNULL(CONCAT(t1.street,', '), ''),
IFNULL(CONCAT(t1.propcode,', '), ''),
IFNULL(CONCAT(t3.name,', '), ''),
IFNULL(CONCAT(t4.name,', '), ''),
IFNULL(CONCAT(t5.name,', '), '')
)
LIKE REPLACE(REPLACE('%Metro Manila, Quezon City%',' ','%'),',','%')
或者您可以使用动态SQL,例如:
DECLARE @SearchStr VARCHAR(1000)
DECLARE @Query VARCHAR(1000)
SET @SearchStr = 'Metro Mangila, Quezon City'
SET @Query = 'SELECT t1.*
, t1.name AS propertyname
, t2.name AS typeName
, t3.name AS provName
, t4.name AS cityName
, t5.name AS brgyName
, t6.imgpath_tn AS imgName
FROM proplistings AS t1
LEFT JOIN proptypes AS t2 ON t2.id = t1.property_type_id
LEFT JOIN tbl_province AS t3 ON t3.id = t1.prov_id
LEFT JOIN tbl_city AS t4 ON t4.id = t1.city_id
LEFT JOIN tbl_barangay AS t5 ON t5.id = t1.brgy_id
LEFT JOIN proplistings_images AS t6 ON t6.propid = t1.id
WHERE t1.deleted = 0
AND t1.announceas = ''1''
AND t6.defaultimg = 1
AND t6.imgpath != ''''
AND t1.status=1
AND (
CONCAT(IFNULL(CONCAT(t1.street,'', ''), ''''),
IFNULL(CONCAT(t1.propcode,'', ''), ''''),
IFNULL(CONCAT(t3.name,'', ''), ''''),
IFNULL(CONCAT(t4.name,'', ''), ''''),
IFNULL(CONCAT(t5.name,'', ''), '''')
) LIKE ''%' + REPLACE(@SearchStr, ' ', '%''
OR
CONCAT(IFNULL(CONCAT(t1.street,'', ''), ''''),
IFNULL(CONCAT(t1.propcode,'', ''), ''''),
IFNULL(CONCAT(t3.name,'', ''), ''''),
IFNULL(CONCAT(t4.name,'', ''), ''''),
IFNULL(CONCAT(t5.name,'', ''), '''')
) LIKE ''%' + '%'')'
EXEC(@Query)