错误:SQLSTATE [42S22]:找不到列:1054

时间:2016-08-24 09:54:09

标签: mysql cakephp-3.0 wampserver

当我运行我的烘焙表(WampServer MySQL数据库)时,我收到此错误:

  

错误:SQLSTATE [42S22]:未找到列:1054'on子句'中的未知列'Users.user_id'

     

如果您使用SQL关键字作为表列名,则可以在config / app.php中为数据库连接启用标识符引用。

SQL查询:

SELECT `Bookmarks`.`id` AS `Bookmarks__id`, 
       `Bookmarks`.`user_id` AS `Bookmarks__user_id`, 
       `Bookmarks`.`title` AS `Bookmarks__title`, 
       `Bookmarks`.`description` AS `Bookmarks__description`, 
       `Bookmarks`.`url` AS `Bookmarks__url`, 
       `Bookmarks`.`created` AS `Bookmarks__created`, 
       `Bookmarks`.`modified` AS `Bookmarks__modified`,
       `Users`.`id` AS `Users__id`, 
       `Users`.`email` AS `Users__email`, 
       `Users`.`password` AS `Users__password`, 
       `Users`.`created` AS `Users__created`, 
       `Users`.`modified` AS `Users__modified` 
FROM `bookmarks` `Bookmarks` 
    INNER JOIN `users` `Users` ON 
            `Users`.`user_id` = (`Bookmarks`.`user_id`) 
LIMIT 20 OFFSET 0

如果要自定义此错误消息,请创建src\Template\Error\pdo_error.ctp

1 个答案:

答案 0 :(得分:1)

在您的用户表格中,主要ID为id而不是user_id;这就是你收到错误的原因。请尝试此查询:

SELECT `Bookmarks`.`id` AS `Bookmarks__id`, 
       `Bookmarks`.`user_id` AS `Bookmarks__user_id`, 
       `Bookmarks`.`title` AS `Bookmarks__title`, 
       `Bookmarks`.`description` AS `Bookmarks__description`, 
       `Bookmarks`.`url` AS `Bookmarks__url`, 
       `Bookmarks`.`created` AS `Bookmarks__created`, 
       `Bookmarks`.`modified` AS `Bookmarks__modified`,
       `Users`.`id` AS `Users__id`, 
       `Users`.`email` AS `Users__email`, 
       `Users`.`password` AS `Users__password`, 
       `Users`.`created` AS `Users__created`, 
       `Users`.`modified` AS `Users__modified` 
FROM `bookmarks` `Bookmarks` 
    INNER JOIN `users` `Users` ON 
            `Users`.`id` = `Bookmarks`.`user_id`
LIMIT 20 OFFSET 0