The poor man’s CRM, a.k.a SuiteCRM, is great (… was great for its time!); that’s another story.
One of the features provided is “unified search”, as the geeks call it; some may call it global/universal search across many tables and records in the database. What it does is simply as follows:
- Loop over a list of modules (e.g. accounts, contacts, opportunities, calls, meetings, notes, etc.)
- Search for records matching a phrase (entered by user) by looking at all the fields
- List the records found under each module
From technical point of view, their solution includes the following logic (e.g. using contact module).
1. Create main query.
(source: include/ListView/listViewData.php function getListViewData())
1 2 3 4 5 6 7 8 9 10 11 12 |
SELECT t1.*, t2.* FROM contacts t1 LEFT JOIN contacts_cstm t2 ON t1.id = t2.id_c WHERE t1.deleted = 0 -- exclude soft-deleted records AND ( ( (t1.first_name LIKE 'john%') OR (t1.last_name LIKE 'john%') -- OR ... using other fields on both tables ) ORDER BY t1.date_modified DESC -- example LIMIT 0, 10 -- first 10 records |
2. Create list count query based on main query by removing ORDER BY and LIMIT clauses.
(source: data/SugarBean.php function create_list_count_query())
1 2 3 |
SELEC COUNT(*) AS c FROM ( -- main query here without ORDER BY and LIMIT clauses ) |
3. Execute list count query and get the total number of records that would be retrieved by main query.
4. Execute main query by using ORDER BY and LIMIT clauses.
(source: include/database/MysqlManager.php function limitQuery())
Loop over the records found, and prepare for display, which may require running additional queries to load related data such as user name of the user who created a selected record.
There are two main issues with this common approach:
A. Too many columns are selected unnecessarily from the tables.
B. We execute more or less same query twice. So-called database abstraction layer and the logic that comes with it prevents us from using a powerful feature of MySQL:
We can modify the main query and inject the key SQL_CALC_FOUND_ROWS; e.g.
1 2 3 4 5 6 7 8 9 10 11 |
SELECT SQL_CALC_FOUND_ROWS t1.*, t2.* FROM contacts t1 LEFT JOIN contacts_cstm t2 ON t1.id = t2.id_c WHERE t1.deleted = 0 -- exclude soft-deleted records AND ( (t1.first_name LIKE 'john%') OR (t1.last_name LIKE 'john%') -- OR ... using other fields on both tables ) ORDER BY t1.date_modified DESC -- example LIMIT 0, 10 -- first 10 records |
After running this version, we can simply extract the total number of records found:
1 |
SELECT found_rows() AS total_records; |
Obviously, this is very specific non-standard feature of MySQL. Still, I think the database abstraction layer should handle this requirement, and implement for each database management system supported.
Next, I want to explain my implementation of global full text search with a simple scoring feature. Let’s start with a very simple example (‘john smith’ is search phrase):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
SELECT * FROM ( SELECT t1.*, LOWER(u.user_name) AS created_by_user_name, ( IF(COALESCE(t1.first_name LIKE '%john%'), 1, 0) + IF(COALESCE(t1.last_name LIKE '%john%'), 1, 0) + IF(COALESCE(t1.first_name LIKE '%smith%'), 1, 0) + IF(COALESCE(t1.last_name LIKE '%smith%'), 1, 0) + ) AS score FROM contacts t1 LEFT JOIN users u ON t1.created_by = u.id LEFT JOIN contacts_cstm t2 ON t1.id = t2.id_c WHERE t1.deleted = 0 -- exclude soft-deleted records ) q WHERE 0 < score ORDER BY score DESC, date_modified DESC |
The idea is this: for each word in the search phrase, create an expression for each column in both main table and custom table; if every match is 1 point; and we simple add the points to calculate an overall score. Then, we can order the records by score calculated in descending order. Also, we can favour the recently modified records when they have same score.
Here is a real example when we search for ‘jo smi ya 18’:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
SELECT * FROM ( SELECT t1.*, LOWER(u.user_name) AS created_by_user_name, ( IF(COALESCE(t1.account_type, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.annual_revenue, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.assigned_user_id, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.billing_address_city, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.billing_address_country, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.billing_address_postalcode, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.billing_address_state, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.billing_address_street, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.campaign_id, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.created_by, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.date_entered, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.date_modified, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.deleted, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.description, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.employees, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.id, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.industry, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.modified_user_id, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.name, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.ownership, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.parent_id, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.phone_alternate, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.phone_fax, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.phone_office, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.rating, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.shipping_address_city, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.shipping_address_country, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.shipping_address_postalcode, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.shipping_address_state, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.shipping_address_street, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.sic_code, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.ticker_symbol, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.website, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.client_overview_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.failures_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.headlines_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_account_bu_owner_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_account_feedback_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_account_overview_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_acc_feedback_last_updated_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_acc_overview_last_updated_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_additional_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_beds_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_business_relationship_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_bu_strategic_target_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_current_opportunities_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_current_opp_last_updated_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_curr_risks_last_updated_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_domain_note_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_existing_customer_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_financial_overview_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_fin_overview_last_updated_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_hidden_import_id_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_last_sync_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_owner_id_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_prev_number_of_won_opps_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_site_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_site_id_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_type_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_uep_e_strategic_account_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_unipart_current_risks_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_vision_statement_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hs_win_people_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.hubspot_id_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.id_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.jjwg_maps_address_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.jjwg_maps_geocode_status_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.jjwg_maps_lat_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.jjwg_maps_lng_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.opportunities_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.public_private_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.ref_code_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.registration_number_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.sector_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.sub_sector_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.success_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t2.threats_c, '') LIKE '%jo%', 1, 0) + IF(COALESCE(t1.account_type, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.annual_revenue, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.assigned_user_id, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.billing_address_city, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.billing_address_country, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.billing_address_postalcode, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.billing_address_state, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.billing_address_street, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.campaign_id, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.created_by, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.date_entered, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.date_modified, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.deleted, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.description, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.employees, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.id, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.industry, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.modified_user_id, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.name, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.ownership, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.parent_id, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.phone_alternate, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.phone_fax, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.phone_office, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.rating, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.shipping_address_city, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.shipping_address_country, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.shipping_address_postalcode, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.shipping_address_state, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.shipping_address_street, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.sic_code, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.ticker_symbol, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.website, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.client_overview_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.failures_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.headlines_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_account_bu_owner_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_account_feedback_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_account_overview_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_acc_feedback_last_updated_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_acc_overview_last_updated_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_additional_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_beds_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_business_relationship_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_bu_strategic_target_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_current_opportunities_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_current_opp_last_updated_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_curr_risks_last_updated_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_domain_note_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_existing_customer_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_financial_overview_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_fin_overview_last_updated_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_hidden_import_id_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_last_sync_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_owner_id_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_prev_number_of_won_opps_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_site_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_site_id_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_type_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_uep_e_strategic_account_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_unipart_current_risks_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_vision_statement_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hs_win_people_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.hubspot_id_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.id_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.jjwg_maps_address_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.jjwg_maps_geocode_status_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.jjwg_maps_lat_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.jjwg_maps_lng_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.opportunities_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.public_private_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.ref_code_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.registration_number_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.sector_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.sub_sector_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.success_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t2.threats_c, '') LIKE '%smi%', 1, 0) + IF(COALESCE(t1.account_type, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.annual_revenue, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.assigned_user_id, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.billing_address_city, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.billing_address_country, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.billing_address_postalcode, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.billing_address_state, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.billing_address_street, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.campaign_id, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.created_by, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.date_entered, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.date_modified, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.deleted, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.description, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.employees, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.id, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.industry, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.modified_user_id, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.name, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.ownership, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.parent_id, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.phone_alternate, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.phone_fax, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.phone_office, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.rating, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.shipping_address_city, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.shipping_address_country, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.shipping_address_postalcode, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.shipping_address_state, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.shipping_address_street, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.sic_code, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.ticker_symbol, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.website, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.client_overview_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.failures_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.headlines_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_account_bu_owner_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_account_feedback_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_account_overview_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_acc_feedback_last_updated_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_acc_overview_last_updated_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_additional_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_beds_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_business_relationship_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_bu_strategic_target_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_current_opportunities_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_current_opp_last_updated_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_curr_risks_last_updated_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_domain_note_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_existing_customer_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_financial_overview_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_fin_overview_last_updated_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_hidden_import_id_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_last_sync_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_owner_id_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_prev_number_of_won_opps_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_site_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_site_id_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_type_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_uep_e_strategic_account_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_unipart_current_risks_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_vision_statement_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hs_win_people_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.hubspot_id_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.id_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.jjwg_maps_address_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.jjwg_maps_geocode_status_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.jjwg_maps_lat_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.jjwg_maps_lng_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.opportunities_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.public_private_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.ref_code_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.registration_number_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.sector_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.sub_sector_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.success_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t2.threats_c, '') LIKE '%ya%', 1, 0) + IF(COALESCE(t1.account_type, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.annual_revenue, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.assigned_user_id, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.billing_address_city, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.billing_address_country, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.billing_address_postalcode, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.billing_address_state, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.billing_address_street, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.campaign_id, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.created_by, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.date_entered, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.date_modified, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.deleted, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.description, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.employees, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.id, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.industry, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.modified_user_id, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.name, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.ownership, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.parent_id, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.phone_alternate, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.phone_fax, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.phone_office, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.rating, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.shipping_address_city, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.shipping_address_country, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.shipping_address_postalcode, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.shipping_address_state, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.shipping_address_street, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.sic_code, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.ticker_symbol, '') LIKE '%18%', 1, 0) + IF(COALESCE(t1.website, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.client_overview_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.failures_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.headlines_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_account_bu_owner_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_account_feedback_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_account_overview_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_acc_feedback_last_updated_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_acc_overview_last_updated_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_additional_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_beds_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_business_relationship_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_bu_strategic_target_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_current_opportunities_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_current_opp_last_updated_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_curr_risks_last_updated_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_domain_note_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_existing_customer_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_financial_overview_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_fin_overview_last_updated_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_hidden_import_id_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_last_sync_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_owner_id_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_prev_number_of_won_opps_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_site_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_site_id_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_type_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_uep_e_strategic_account_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_unipart_current_risks_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_vision_statement_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hs_win_people_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.hubspot_id_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.id_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.jjwg_maps_address_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.jjwg_maps_geocode_status_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.jjwg_maps_lat_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.jjwg_maps_lng_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.opportunities_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.public_private_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.ref_code_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.registration_number_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.sector_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.sub_sector_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.success_c, '') LIKE '%18%', 1, 0) + IF(COALESCE(t2.threats_c, '') LIKE '%18%', 1, 0)) ) AS score FROM accounts t1 LEFT JOIN users u ON t1.created_by = u.id LEFT JOIN accounts_cstm t2 ON t1.id = t2.id_c WHERE t1.deleted = 0 ) q WHERE 0 < score ORDER BY score DESC, date_modified DESC LIMIT 50 OFFSET 0; |