【问题标题】:Unknown column username in 'where clause''where 子句'中的未知列用户名
【发布时间】:2013-02-16 21:15:24
【问题描述】:

当我尝试在表上选择其中一列是外键时出现此错误:

select count(*) from cards where username = 'name';

我是通过 java jdbc 做的,所以我认为我的映射有问题,因为通过 MySql 命令行的相同查询工作正常:

<many-to-one name="users" class="table" update="false" insert="false" fetch="select">
        <column name="username" length="45" not-null="true" />
</many-to-one>

2013-03-02 12:19:03,660  INFO [http-bio-8080-exec-5] (NullableType.java:203) - could not read column value from result set: username; Column 'username' not found.
2013-03-02 12:19:03,663  WARN [http-bio-8080-exec-5] (JDBCExceptionReporter.java:100) - SQL Error: 0, SQLState: S0022
2013-03-02 12:19:03,664 ERROR [http-bio-8080-exec-5] (JDBCExceptionReporter.java:101) - Column 'username' not found.

我发现了这个:

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html#d0e13696

第 16.1.2 和 16.1.3 节可能是我问题的答案,但我想不通...

【问题讨论】:

  • (1) 检查您是否从正确的表格中选择。 (2) 检查您是否连接到正确的数据库。
  • 是的,我支持 1 和 2

标签: java sql hibernate jdbc hibernate-mapping


【解决方案1】:

错误信息指出:

无法从结果集中读取列值:用户名;未找到“用户名”列。

select count(*) from ... 的结果集没有名为 username 的列。它只包含一列,即计数。

【讨论】:

  • @user1692261:我不确定我是否遵循。您将'name' 提供给查询,然后取回该名称的计数
  • 这是多对一表。对于每个用户名属性,此表中可能有几行。例如,我想计算用户名 NPE 的表中有多少行。
【解决方案2】:

尝试执行

 select count(*) from cards where `cards`.username='name';

【讨论】:

    【解决方案3】:

    获取字段不应使用“Select *”。
    应该使用“从用户名='名称'的卡片中选择计数(用户名);”表达。

    【讨论】:

      猜你喜欢
      • 2018-01-16
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      • 2021-09-19
      • 2020-06-28
      • 2021-07-02
      • 2019-08-14
      相关资源
      最近更新 更多