busboy — 2012-11-12T15:57:47-05:00 — #1
I use this simple query to determine the most popular searches on my website:
select keyword, count(*) as total from searches group by keyword order by total desc limit 500;
I want to add a column that indicates how many times a particular keyword is found in testimonials. The testimonials are in a separate table called testimonies. I'm thinking that I need to implement the following, but I don't know exactly where:
count( case when (t.title like '%s.keyword%' or t.testimonyText like '%s.keyword%') then 1 else null end ) as countInTestimonials
So how do I modify the searches query to include a count in the testimonies table?
r937 — 2012-11-13T05:20:14-05:00 — #2
why does the testimonials query have to be combined with the searches query?
how are the two tables related?
busboy — 2012-11-13T12:09:56-05:00 — #3
Oh, you're right. I guess they don't have to be combined into one query. So you are suggesting that in my PHP code, in the Do loop, for every keyword that is returned from the first query to then run the second query? To answer your question, the two tables are related in that they are both in the same database.
r937 — 2012-11-13T13:47:03-05:00 — #4
no, not at all
, count(*) as total
, ( SELECT COUNT(*)
WHERE title LIKE CONCAT('%',s.keyword,'%')
OR testimonyText LIKE CONCAT('%',s.keyword,'%') ) AS countInTestimonials
FROM searches AS s
BY total desc limit 500;
busboy — 2012-11-13T16:24:42-05:00 — #5
This query never finishes. It timed out in the two times I tried to run it. Maybe the 240,000 rows in the searches table and 7,000 rows in the testimonies table is too much?
r937 — 2012-11-13T18:35:46-05:00 — #6
maybe you gotta ask yourself why you want to see both pieces of information at the same time
also, don't go the route of doing a query inside a loop -- that's gonna be just as slow