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())


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())


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:

function found_rows()

We can modify the main query and inject the key SQL_CALC_FOUND_ROWS; e.g.


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:


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):


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':


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;