【问题标题】:issue with derby database on where conditionderby 数据库在 where 条件下的问题
【发布时间】:2013-12-04 05:48:41
【问题描述】:

下面是我的表结构。

create table "APP".REGISTRATION
(
        "id" INT not null primary key GENERATED ALWAYS AS IDENTITY(START WITH 1,INCREMENT BY 1),
        "firstname" VARCHAR(50),
        "lastname" VARCHAR(50),
        "username" VARCHAR(50),
        "password" VARCHAR(50),
        "email" VARCHAR(50)
);

我插入了一行:

firstname=susheel lastname=singh username=susheel61 password=password email=test@gmail.com

现在当我尝试查询为

select * from REGISTRATION where USERNAME='susheel61';

我收到一条错误消息:

> Error code 0, SQL state 42X04: Column 'USERNAME' is either not in any
> table in the FROM list or appears within a join specification and is
> outside the scope of the join specification or appears in a HAVING
> clause and is not in the GROUP BY list. If this is a CREATE or ALTER
> TABLE  statement then 'USERNAME' is not a column in the target table.

【问题讨论】:

  • "username" 是与 username 不同的列名。带引号的标识符在 SQL 中区分大小写。

标签: java sql derby


【解决方案1】:

对不起,我无法复制这个。这是ij 的快速会议:

ij> create table "APP".REGISTRATION
> (
>   id INTEGER generated by default as identity (start with 1, increment by 1) not null primary key,
>   firstname VARCHAR(50),
>   lastname VARCHAR(50),
>   username VARCHAR(50),
>   password VARCHAR(50),
>   email VARCHAR(50)
> );
0 rows inserted/updated/deleted
ij> insert into "APP".registration (firstname, lastname, username, password, email) values ('susheel', 'singh', 'susheel61', 'password', 'test@gmail.com');
1 row inserted/updated/deleted
ij> select * from REGISTRATION where USERNAME='susheel61';
ID         |FIRSTNAME                                         |LASTNAME                                          |USERNAME               |PASSWORD                                          |EMAIL
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1          |susheel                                           |singh                                             |susheel61              |password                                          |test@gmail.com

1 row selected
ij>

请注意,我必须更改您的 CREATE TABLE 语句才能创建表。您问题中的CREATE TABLE 语句无效;当我尝试运行它时出现“语法错误”。

编辑:现在您已经提供了用于创建表的实际 SQL 脚本,我可以解释其中的区别。

不同之处在于您在列名周围指定了双引号。这样做会使列名区分大小写。然后在查询表时必须使用双引号,除非列名都是大写的。如果您没有在名称周围指定双引号,则该名称将被视为全部大写。

以下是按照最初指定的方式查询表的方法:

ij> create table "APP".REGISTRATION
> (
>   "id" INT not null primary key GENERATED ALWAYS AS IDENTITY(START WITH 1,INCREMENT BY 1),
>   "firstname" VARCHAR(50),
>   "lastname" VARCHAR(50),
>   "username" VARCHAR(50),
>   "password" VARCHAR(50),
>   "email" VARCHAR(50)
> );
0 rows inserted/updated/deleted
ij> select * from "APP".registration where username = 'susheel61';
ERROR 42X04: Column 'USERNAME' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE  statement then 'USERNAME' is not a column in the target table.
ij> select * from "APP".registration where "username" = 'susheel61';
id         |firstname                                         |lastname                                          |username                                                  |password                                          |email
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

0 rows selected
ij>

(这次我没有费心插入任何数据,但希望您仍能明白这一点:查询完成且没有错误。)

请注意,这次的列标题是小写的,而上面输出的第一部分中的列标题是大写的。

【讨论】:

  • 你在 derby 数据库中检查这个
【解决方案2】:

在我的情况下,发生此错误是因为我在 WHERE 语句中使用双引号而不是单引号

【讨论】:

    【解决方案3】:

    我无法获得表的创建语句,并且描述表没有说明我的问题,但我必须用双引号将所有表名和列名括起来,并用单引号括起值。

    SELECT "name" FROM "THING_TABLE" WHERE "name"='Environments';
    

    有趣的是,当我使用 ID 列时,我不需要引号。不知道为什么。

    可能是创建表的方式。上面说单引号表示表名区分大小写?

    【讨论】:

      【解决方案4】:

      这是我用来创建注册表的脚本,它已成功创建,但在使用 where 条件查询表时出现问题。

      create table "APP".REGISTRATION
      (
              "id" INT not null primary key GENERATED ALWAYS AS IDENTITY(START WITH 1,INCREMENT BY 1),
              "firstname" VARCHAR(50),
              "lastname" VARCHAR(50),
              "username" VARCHAR(50),
              "password" VARCHAR(50),
              "email" VARCHAR(50)
      );
      

      现在我将其更改为创建表的语法,一切正常。 不知道是什么原因。

      create table "APP".REGISTRATION
      (
          id INTEGER generated by default as identity (start with 1, increment by 1) not null primary key,
          firstname VARCHAR(50),
          lastname VARCHAR(50),
          username VARCHAR(50),
          password VARCHAR(50),
          email VARCHAR(50) unique
      );
      

      【讨论】:

        猜你喜欢
        • 2016-07-12
        • 2019-07-01
        • 1970-01-01
        • 2021-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        相关资源
        最近更新 更多