SELECT nmemail as order_email,
dtorder,
vlOrder,
cohorts.cohortdate
FROM factorderline
JOIN (SELECT nmemail as cohort_email, Min(dtorder) AS cohortDate FROM factorderline GROUP BY cohort_email limit 5) cohorts
ON order_email= cohort_email limit 5;
错误:列“order_email”不存在
此查询有什么问题?
答案 0 :(得分:1)
问题很可能是在评估连接时没有解析列别名的定义;请改用实际的列名:
function connect()
{
$info = 'mysql:host='.$this->dbHost.';dbname='.$this->dbName;
try
{
$this->con = new PDO($info, $this->dbUser, $this->dbPass);
}
catch(PDOException $e)
{
print "Error Founds: ".$e->getMessage().PHP_EOL;
die();
}
return $this->con;
}
此外,使用SELECT nmemail as order_email,
dtorder,
vlOrder,
cohorts.cohortdate
FROM factorderline
JOIN (
SELECT nmemail as cohort_email, Min(dtorder) AS cohortDate
FROM factorderline
GROUP BY cohort_email limit 5
) cohorts ON nmemail = cohort_email
limit 5;
时,您确实应该使用limit
子句。
来自文档:
使用LIMIT时,使用ORDER BY子句很重要 将结果行限制为唯一的顺序。否则你会得到 查询行的不可预知的子集。
答案 1 :(得分:1)
问题是输出列名称不能用于连接。
输出列的名称可用于引用ORDER BY和GROUP BY子句中的列值,但不能用于WHERE或HAVING子句中;那里你必须写出表达式。