Skip to content

Instantly share code, notes, and snippets.

@tcocca
Created November 4, 2010 20:02
Show Gist options
  • Save tcocca/663089 to your computer and use it in GitHub Desktop.
Save tcocca/663089 to your computer and use it in GitHub Desktop.
SELECT leads.id,
(SELECT COUNT(*) FROM searches WHERE lead_id = leads.id) AS searches_count,
(SELECT COUNT(*) FROM rental_searches WHERE lead_id = leads.id) AS rental_searches_count,
COALESCE((searches_count + rental_searches_count), 0) as total
FROM leads
WHERE leads.id = 35
# Unknown column 'searches_count' in 'field list'
@kevinzen
Copy link

kevinzen commented Nov 4, 2010

SELECT leads.id,
(SELECT COUNT() FROM searches WHERE lead_id = 35) AS searches_count,
(SELECT COUNT(
) FROM rental_searches WHERE lead_id = 35) AS rental_searches_count,
COALESCE((searches_count + rental_searches_count), 0) as total
FROM leads
WHERE leads.id = 35

@kevinzen
Copy link

kevinzen commented Nov 4, 2010

If you're really doing only running this query at any one time for a specific lead.id then it's probably better to just specify the id in the sub-queries. Also, make sure you have indexes on lead_id on the searches and rental_searches table.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment