我使用jdbc运行此查询并成功在MySQL中获得结果:
SELECT COUNT(DISTINCT xx.x, xx.z) as `n_x|z`,
COUNT(DISTINCT xx.w, xx.y, xx.z) as `n_w|y|z` /*,...same COUNTS*/
FROM (SELECT x0.field0 as `x`, x0.field1 as `y`, x0.field2 as `z`,
x0.field3 as `w` FROM `Kind` x0 ) as xx
以下查询也成功生成结果:
SELECT (1 * -1.5) as weight,
xx.x, xx.y, xx.z, xx.w
/*The same table:*/
FROM (SELECT x0.field0 as `x`, x0.field1 as `y`, x0.field2 as `z`,
x0.field3 as `w` FROM `Kind` x0 ) as xx
但是组合:
SELECT COUNT(DISTINCT xx.x, xx.z) as `n_x|z`,
COUNT(DISTINCT xx.w, xx.y, xx.z) as `n_w|y|z` /*,... same COUNTs*/
,(1 * -1.5) as weight,
xx.x, xx.y, xx.z, xx.w
/*The same table:*/
FROM (SELECT x0.field0 as `x`, x0.field1 as `y`, x0.field2 as `z`,
x0.field3 as `w` FROM `Kind` x0 ) as xx
犯了错误:
您的SQL语法有错误;检查手册 对应于您的MySQL服务器版本,以便使用正确的语法 靠近' xx.x,xx.y,xx.z,xx.w FROM(SELECT x0.field0 as
x
,x0.field1 如y
,x0.fie'在第1行
即使删除,(1 * -1.5) as weight
也无济于事!
我可以单独运行两个查询,但我更喜欢将它们组合起来,以便从复杂耗时的查询中获取表:
FROM (SELECT x0.field0 as `x`, x0.field1 as `y`, x0.field2 as `z`, x0.field3 as `w` FROM `Kind` x0 ) as xx"
生成一次。 我做错了什么?
答案 0 :(得分:0)
小心你在第一个查询中有一个关于行总数的聚合
在第二个中你有没有任何聚合的行
您从两个查询中选择不同数量的行 所以你应该为非聚合函数或者分组,例如:
SELECT COUNT(DISTINCT xx.x, xx.z) as `n_x|z`,
COUNT(DISTINCT xx.w, xx.y, xx.z) as `n_w|y|z`
,(1 * -1.5) as weight,
xx.x, xx.y, xx.z, xx.w
FROM (SELECT x0.field0 as `x`, x0.field1 as `y`, x0.field2 as `z`,
x0.field3 as `w` FROM `Kind` x0 ) as xx
group by weight, xx.x, xx.y, xx.z, xx.w
或(取决于您的应用程序需求)在不平行的行上使用聚合函数(例如:min())
SELECT COUNT(DISTINCT xx.x, xx.z) as `n_x|z`,
COUNT(DISTINCT xx.w, xx.y, xx.z) as `n_w|y|z`,
min((1 * -1.5)) as weight,
min(xx.x),
min(xx.y),
min(xx.z),
min(xx.w)
FROM (SELECT x0.field0 as `x`, x0.field1 as `y`, x0.field2 as `z`,
x0.field3 as `w` FROM `Kind` x0 ) as xx
group by weight, xx.x, xx.y, xx.z, xx.w
或者如同您在上次评论中一样,如果您希望重复计算xx中的所有行,您可以使用CROSS JOIN
SELECT c_xx.`n_x|z`, c_xx.`n_w|y|z`,
(1 * -1.5) as weight,
xx1.x, xx1.y, xx1.z, xx1.w
FROM (SELECT x0.field0 as `x`, x0.field1 as `y`, x0.field2 as `z`,
x0.field3 as `w` FROM `Kind` x0 ) xx1
CROSS JOIN (
SELECT COUNT(DISTINCT xx.x, xx.z) as `n_x|z`,
COUNT(DISTINCT xx.w, xx.y, xx.z) as `n_w|y|z` //,... same COUNTS
FROM (SELECT x0.field0 as `x`, x0.field1 as `y`, x0.field2 as `z`,
x0.field3 as `w` FROM `Kind` x0 ) xx
) c_xx