Mysql SQL脚本未按预期工作:如何在虚拟列中使用聚合对结果排序?
我有这个:Mysql SQL脚本未按预期工作:如何在虚拟列中使用聚合对结果排序?,mysql,sql,Mysql,Sql,我有这个: 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_
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;
我想我想要的是这样的东西:
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;
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;
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
http_status_code = 200
)
HAVING total_social > 865.0 ORDER BY total_social DESC;
唯一的问题是MySQL Workbench说,total_social
不存在
我试过使用这样的CTE:
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;
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;
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
http_status_code = 200
)
HAVING total_social > 865.0 ORDER BY total_social DESC;
就像在“”中一样,但MySQL Workbench不会接受它,因为它不喜欢在该位置带有的
,我知道现在MySQL不支持它。对,所以解决方案是使用我从“”中找到的HAVING
子句,我认为这基本上是一个WHERE
子句,发生在SELECT
之后
现在,我的SQL如下所示:
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;
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;
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
http_status_code = 200
)
HAVING total_social > 865.0 ORDER BY total_social DESC;
现在它执行查询、选择、计算total\u social
列,然后使用total\u social
执行第二次查询,然后按total\u social
降序排序 毫无疑问,有了样本数据和期望的结果,其他人可能会知道您的实际意图。您不需要变量:select。。。在哪里((comments_html+…+linked_in)>865))
那么我如何将其传递给ORDER BY?另外,它不存在于行查询之外,这就是为什么我在查询之外执行它。我想这可能就是我想要的。@MarcB我猜OP正在使用变量,这样他就不必在orderby子句中重新写这行了。就我个人而言,我会在select子句中创建一个变量,就像他在第二个版本中创建的一样。在这一点上,我和Gordon是一致的,我们需要一些样本数据(或者数据来自的网站,我猜他使用的是google scholar之类的),以及他期望的,以及他得到的。