I have this:
SELECT * FROM `npt_articles` WHERE (date_discovered >= 1464534366 AND
date_discovered <= 1464534366 AND
npt_site_id = 4 AND
(@total_social := (comments_html + fb_total + google_plus_one + pinterest + linked_in ) + 0.0) >
865.0 AND
http_status_code = 200) ORDER BY @total_social DESC;
It works but no where near as intended.
total_social
is the aggregate value of other named fields of the same row (record, object, etc.) which is then compared against a number which is injected into the query string. I want to also use this aggregate in a virtual column to then be used to order the results.
I tried this but I'm not sure if this is the way to go:
SELECT *, @total_social := (comments_html + fb_total + google_plus_one + pinterest + linked_in ) + 0.0 FROM `npt_articles`
WHERE (date_discovered >= 1464534366 AND
npt_site_id = 4 AND
@total_social >
865.0 AND
http_status_code = 200) ORDER BY @total_social DESC;
I think what I want is something like this:
SELECT *, ((comments_html + fb_total + google_plus_one + pinterest + linked_in ) + 0.0) as total_social FROM `npt_articles` WHERE (date_discovered >= 1464534366 AND
npt_site_id = 4 AND
total_social >
865.0 AND
http_status_code = 200) ORDER BY total_social DESC;
The only issue is that MySQL Workbench says total_social
doesn't exist.
I've tried using a CTE like this:
WITH inner_table AS (
SELECT
((comments_html + fb_total + google_plus_one + pinterest + linked_in) + 0.0) AS total_social
FROM
`npt_articles`
)
select * FROM inner_table
WHERE
(date_discovered >= 1464534366
AND npt_site_id = 4
AND total_social > 865.0
AND http_status_code = 200)
ORDER BY total_social DESC;
like in "Referring to a Column Alias in a WHERE Clause" but MySQL Workbench won't accept it because it doesn't like the WITH
at that position, which I know now isn't supported by MySQL.