我的查询是这样的:
SELECT t1.customer_name, t1.total, t2.customer_name, t2.total,
(t1.total - t2.total) as var_1
FROM topuserslcy t1, topuserslcy t2
WHERE t1.report_date = '2015-12-22' AND
t2.report_date = '2015-12-23' AND var_1 > 0;
我希望它只选择值或var_1为正或大于零的行。 Thnaks
答案 0 :(得分:0)
您不能在from
子句中使用列别名。我强烈怀疑你需要适当的join
条件。 。 。作为一般规则,从不在from
子句中使用逗号。
MySQL具有允许在having
子句中使用别名的功能。因此,如果编写为:
SELECT t1.customer_name, t1.total, t2.customer_name, t2.total,
(t1.total - t2.total) as var_1
FROM topuserslcy t1 CROSS JOIN
topuserslcy t2
WHERE t1.report_date = '2015-12-22' AND
t2.report_date = '2015-12-23'
HAVING var_1 > 0;
答案 1 :(得分:0)
ANSI SQL兼容的答案,使用派生表。检查顶级var_1:
select * from
(
SELECT t1.customer_name, t1.total, t2.customer_name, t2.total,
(t1.total - t2.total) as var_1
FROM topuserslcy t1, topuserslcy t2
WHERE t1.report_date = '2015-12-22' AND
t2.report_date = '2015-12-23'
) dt
WHERE var_1 > 0;
(但为什么没有JOIN条件?)
答案 2 :(得分:0)
试试这个:
SELECT temp.* FROM (SELECT t1.customer_name, t1.total, t2.customer_name, t2.total,
(t1.total - t2.total) as var_1
FROM topuserslcy t1, topuserslcy t2
WHERE t1.report_date = '2015-12-22' AND
t2.report_date = '2015-12-23') as temp WHERE temp.var_1 > 0;