I’m trying to find a way to create a calculation from results in a database query to provide a sort of relevance.
Here’s an example of my query:
SELECT d.id, MATCH (r.text) AGAINST('sleet snow rain' IN BOOLEAN MODE) as r_matches, s.total_matches
FROM days d
LEFT JOIN condition_days r
ON (d.id = r.day_id AND MATCH (r.text) AGAINST('sleet snow rain' IN BOOLEAN MODE))
LEFT JOIN (SELECT ss.day_id, COUNT(DISTINCT ss.condition_id) as total_matches FROM conditions ss WHERE ss.condition_id IN (4, 13, 20) GROUP BY ss.day_id) s
ON (s.day_id = d.id)
Which returns something like this:
+-----+-----------+---------------+
| id | r_matches | total_matches |
+-----+-----------+---------------+
| 540 | 2 | 5 |
+-----+-----------+---------------+
So my question is, how do I get a calculation of the 2 calculated fields (r_matches and total_matches)?
This is an example of what I’m looking for:
+-----+-----------+---------------+----------------------------------------------+
| id | r_matches | total_matches | total |
+-----+-----------+---------------+----------------------------------------------+
| 540 | 2 | 3 | 5 (calculation of total_matches + r_matches) |
+-----+-----------+---------------+----------------------------------------------+
So is there a way to get a calculated total from 2 calculated fields?
Can’t test run without schemas, but something like this using temporary variables should do it just fine;