2

我正在使用 cakePHP 创建一个待办事项应用程序。CakePHP 会为您创建查询等。这就是为什么不能有错字的原因。

错误:

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Projecttask.projecttasks_name' in 'field list'

查询:

SQL Query: SELECT `Itemrequirement`.`itemreq_id`, `Projecttask`.`projecttasks_name` 
FROM `gtd`.`itemrequirements` AS `Itemrequirement` 
LEFT JOIN `gtd`.`projecttasks` AS `ProjecttaskParent` ON (`Itemrequirement`.`itemreqs_rel_projectparents` = `ProjecttaskParent`.`projecttasks_id`)    
LEFT JOIN `gtd`.`projecttasks` AS `ProjecttaskChild` ON (`Itemrequirement`.`itemreqs_rel_projectchilds` = `ProjecttaskChild`.`projecttasks_id`) 
WHERE 1 = 1 
ORDER BY `Itemrequirement`.`itemreq_id` asc LIMIT 10000

数据库: 数据库的一部分

我越来越无能为力了,因为我在 phpmyadmin 中手动尝试了很多东西..

4

3 回答 3

1

您正在使用projecttasks两次

LEFT JOIN `gtd`.`projecttasks` AS `ProjecttaskParent` ...
LEFT JOIN `gtd`.`projecttasks` AS `ProjecttaskChild`  ...

但使用别名ProjecttaskParentand ProjecttaskChild,因此您必须使用别名而不是表名

`ProjecttaskParent`.`projecttasks_name`

或者

`ProjecttaskChild`.`projecttasks_name`

您的查询应如下所示(以ProjecttaskChild别名为例)

SELECT `Itemrequirement`.`itemreq_id`, `ProjecttaskChild`.`projecttasks_name` 
FROM `gtd`.`itemrequirements` AS `Itemrequirement` 
LEFT JOIN `gtd`.`projecttasks` AS `ProjecttaskParent` ON (`Itemrequirement`.`itemreqs_rel_projectparents` = `ProjecttaskParent`.`projecttasks_id`)    
LEFT JOIN `gtd`.`projecttasks` AS `ProjecttaskChild` ON (`Itemrequirement`.`itemreqs_rel_projectchilds` = `ProjecttaskChild`.`projecttasks_id`) 
WHERE 1 = 1 
ORDER BY `Itemrequirement`.`itemreq_id` asc LIMIT 10000
于 2013-09-19T10:29:06.623 回答
0

没有项目任务

从错误消息中:

“字段列表”中的未知列“Projecttask.projecttasks_name”

所以有一个表/别名Projecttask和一个字段projecttasks_name。除了在查询中:

FROM `gtd`.`itemrequirements` AS `Itemrequirement` 
LEFT JOIN `gtd`.`projecttasks` AS `ProjecttaskParent` ...   
LEFT JOIN `gtd`.`projecttasks` AS `ProjecttaskChild` ...

该表有两个不同的别名,即问题不在于projecttasks中不存在该字段,而是查询中没有具有该名称/别名的表。

您想要的查询可能是:

SELECT 
    `Itemrequirement`.`itemreq_id`, 
    `ProjecttaskParent`.`projecttasks_name` # <- alias updated
FROM 
    `gtd`.`itemrequirements` AS `Itemrequirement` 
LEFT JOIN 
    `gtd`.`projecttasks` AS `ProjecttaskParent` ON (...)    
LEFT JOIN 
    `gtd`.`projecttasks` AS `ProjecttaskChild` ON (...) 
WHERE 
    1 = 1 
ORDER BY 
    `Itemrequirement`.`itemreq_id` asc 
LIMIT 10000

PHP代码?

查询代码未显示,但您需要更改

$Itemrequirement->find('all', array(
    'conditions' => array(
        'Projecttask.projecttask_name' ...

$Itemrequirement->find('all', array(
    'conditions' => array(
        'ProjecttaskParent.projecttask_name' ...

或者使用escapeField来确保别名正确

$Itemrequirement->find('all', array(
    'conditions' => array(
        $Itemrequirement->ProjecttaskParent->escapeField('projecttask_name') ...
于 2013-09-19T10:33:09.720 回答
0

您正在从数据库名称 ProjectTask 中选择,并使用别名(ProjectTaskParent 和 ProjectTaskChild)

您的查询应该是:

SELECT `Itemrequirement`.`itemreq_id`, `ProjecttaskParent`.`projecttasks_name` FROM `gtd`.`itemrequirements` AS `Itemrequirement` LEFT JOIN `gtd`.`projecttasks` AS `ProjecttaskParent` ON (`Itemrequirement`.`itemreqs_rel_projectparents` = `ProjecttaskParent`.`projecttasks_id`) LEFT JOIN `gtd`.`projecttasks` AS `ProjecttaskChild` ON (`Itemrequirement`.`itemreqs_rel_projectchilds` = `ProjecttaskChild`.`projecttasks_id`) WHERE 1 = 1 ORDER BY `Itemrequirement`.`itemreq_id` asc LIMIT 10000

改变Projecttaskprojecttasks_nameProjecttaskParent. projecttasks_nameProjecttaskChildprojecttasks_name

于 2013-09-19T10:30:46.860 回答