If I'm going to search a city address for example, "Quezon City", there's a result. But if I'll search city address and province like "Quezon City, Metro Manila", no result which supposedly there is.
Here's my sql query as of the moment.
//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
If you concat and any of the columns contain null then the result is null. Try coding for null.
For example
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)
I found 2 issues.
I see that you concatenate in following order: ... + province + city + ...
So if there Quezon City
is city and Metro Manila
is province - results of concatenation looks like: Metro Manila, Quezon City
so you have to use LIKE '%Metro Manila, Quezon City%'
instead of LIKE '%Quezon City, Metro Manila%'
.
Also you have to use IFNULL
to avoid concatenation of nulls, that because if any of value in CONCAT
will be NULL
entire result will be NULL
.
So your WHERE
clause should be like this:
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%',' ','%'),',','%')
Or you could use Dynamic SQL, something like:
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.