【问题标题】:Error: Unknown column 'catId' in 'from clause'错误:“from 子句”中的未知列“catId”
【发布时间】:2012-02-29 10:50:05
【问题描述】:

我已经在 WAMP 上运行了这个脚本,它运行良好。尝试上传时出现错误,不确定它的真正含义。

SELECT *, COUNT(DISTINCT forum_thread.threadId) AS 'threadTotal', COUNT(DISTINCT forum_posts.postId) AS 'postTotal'
FROM forum_cat
LEFT JOIN forum_posts USING (catId)
LEFT JOIN forum_thread USING (catId)
    LEFT JOIN users ON users.userId = forum_posts.userId
GROUP BY forum_cat.catId

这是在 WAMP 中运行良好的 SQL 脚本;在线我收到以下错误

注意:查询:SELECT *, COUNT(DISTINCT forum_thread.threadId) AS 'threadTotal', COUNT(DISTINCT forum_posts.postId) AS 'postTotal' FROM forum_cat LEFT JOIN forum_posts USING (catId) LEFT JOIN forum_thread USING (catId) LEFT在 users.userId = forum_posts.userId GROUP BY catId
上加入用户 MySQL 错误:“from 子句”中的未知列“catId”

【问题讨论】:

  • 我怀疑这是一个 MyISAM 表,您从不区分大小写的系统 (win) 上传到区分大小写的系统 (linux)。在目标系统上运行DESC forum_catDESC forum_postsDESC forum_thread,并留意CatIdcatID 或朋友。
  • @alex 抱歉是 forum_cat.catId

标签: php mysql


【解决方案1】:

错误告诉你出了什么问题。没有这样的专栏。可能您未能准确地重新创建表架构。

【讨论】:

    【解决方案2】:

    可能是您的 catid 列在您加入的多个表中存在冲突,因此请使用带有列名的 alias

    试试下面:

    SELECT *, COUNT(DISTINCT ft.threadId) AS 'threadTotal', COUNT(DISTINCT fp.postId) AS 'postTotal'
    FROM forum_cat as fc
    LEFT JOIN forum_posts as fp USING(fp.catId)
    LEFT JOIN forum_thread as ft USING(ft.catId)
        LEFT JOIN users ON users.userId = fp.userId
    GROUP BY fc.catId
    

    【讨论】:

    • 如果我使用它会出错。我稍微改变了我的并删除了 forum_posts 并且它返回正常。我认为它可能是冲突的 USING(catId) 'SELECT *, COUNT(DISTINCT forum_thread.threadId) AS 'threadTotal' FROM forum_cat LEFT JOIN forum_thread USING (catId) LEFT JOIN users ON forum_thread.userId = users.userId GROUP BY forum_cat.catId ORDER BY forum_cat.catId ASC' 这是修改后的代码,但显然不是我想要的
    • 注意:查询:SELECT *, COUNT(DISTINCT ft.threadId) AS 'threadTotal', COUNT(DISTINCT fp.postId) AS 'postTotal' FROM forum_cat as fc LEFT JOIN forum_posts as fp USING (fp .catId) LEFT JOIN forum_thread as ft USING (ft.catId) LEFT JOIN users ON users.userId = forum_posts.userId GROUP BY fc.catId MySQL 错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 /home/scotty/public_html/forums 的第 3 行中的 '.catId) LEFT JOIN forum_thread as ft USING (ft.catId) LEFT JOIN users ON use' 附近使用正确的语法/index.php 第 17 行
    • 尝试使用on子句而不是使用
    • 使用 on 子句我得到 'Notice: Query: SELECT *, COUNT(DISTINCT forum_thread.threadId) AS 'threadTotal', COUNT(DISTINCT forum_posts.postId) AS 'postTotal' FROM forum_cat LEFT JOIN forum_thread ON forum_thread.catId = forum_cat.catId LEFT JOIN forum_posts ON forum_posts.catId = forum_thread.catId LEFT JOIN users ON forum_thread.userId = users.userId GROUP BY forum_cat.catId ORDER BY forum_cat.catId ASC MySQL错误:未知列'forum_posts.catId ' 在 /home/scotty/public_html/forums/index.php 第 18 行的 'on 子句'中'
    • 查看列名是 catId 还是 catid? “我”大写。让它和你在桌子上的一样
    猜你喜欢
    • 2013-01-26
    • 1970-01-01
    • 2012-04-25
    • 2015-09-11
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    相关资源
    最近更新 更多