Hsqldb-where-clause
提供:Dev Guides
HSQLDB-Where句
通常、SELECTコマンドを使用してHSQLDBテーブルからデータを取得します。 WHERE条件節を使用して、結果のデータをフィルター処理できます。 WHEREを使用して、選択基準を指定して、テーブルから必要なレコードを選択できます。
構文
HSQLDBテーブルからデータを取得するSELECTコマンドのWHERE句の構文は次のとおりです。
SELECT field1, field2,...fieldN table_name1, table_name2...
[WHERE condition1 [AND [OR]] condition2.....
- コンマで区切られた1つ以上のテーブルを使用して、WHERE句を使用してさまざまな条件を含めることができますが、WHERE句はSELECTコマンドのオプション部分です。
- WHERE句を使用して、任意の条件を指定できます。
- ANDまたはOR演算子を使用して、複数の条件を指定できます。
- WHERE句をDELETEまたはUPDATE SQLコマンドとともに使用して、条件を指定することもできます。
条件を使用してレコードデータをフィルタリングできます。 条件付きWHERE句で異なる演算子を使用しています。 WHERE句で使用できる演算子のリストを次に示します。
Operator | Description | Example |
---|---|---|
= | Checks if the values of two operands are equal or not, if yes then the condition becomes true. | (A = B) is not true |
!= | Checks if the values of two operands are equal or not, if values are not equal then the condition becomes true. | (A != B) is true |
> | Checks if the value of the left operand is greater than the value of the right operand, if yes then the condition becomes true. | (A > B) is not true |
< | Checks if the value of the left operand is less than the value of the right operand, if yes then the condition becomes true. | (A < B) is true |
>= | Checks if the value of the left operand is greater than or equal to the value of the right operand, if yes then the condition becomes true. | (A >= B) is not true |
⇐ | Checks if the value of the left operand is less than or equal to the value of the right operand, if yes then the condition becomes true. | (A ⇐ B) is true |
例
ID、タイトル、「Learn C」というタイトルの本の著者などの詳細を取得する例を次に示します。 SELECTコマンドでWHERE句を使用することで可能です。 以下は同じクエリです。
SELECT id, title, author FROM tutorials_tbl WHERE title = 'Learn C';
上記のクエリを実行すると、次の出力が表示されます。
+------+----------------+-----------------+
| id | title | author |
+------+----------------+-----------------+
| 101 | Learn C | Yaswanth |
+------+----------------+-----------------+
HSQLDB – JDBCプログラム
以下に、タイトル Learn C を持つtutorials_tblテーブルからレコードデータを取得するJDBCプログラムを示します。 次のコードを WhereClause.java に保存します。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class WhereClause {
public static void main(String[] args) {
Connection con = null;
Statement stmt = null;
ResultSet result = null;
try {
Class.forName("org.hsqldb.jdbc.JDBCDriver");
con = DriverManager.getConnection(
"jdbc:hsqldb:hsql://localhost/testdb", "SA", "");
stmt = con.createStatement();
result = stmt.executeQuery(
"SELECT id, title, author FROM tutorials_tbl
WHERE title = 'Learn C'");
while(result.next()){
System.out.println(result.getInt("id")+" |
"+result.getString("title")+" |
"+result.getString("author"));
}
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
}
次のコマンドを使用して、データベースを起動できます。
\>cd C:\hsqldb-2.3.4\hsqldb
hsqldb>java -classpath lib/hsqldb.jar org.hsqldb.server.Server --database.0
file:hsqldb/demodb --dbname.0 testdb
次のコマンドを使用して、上記のコードをコンパイルおよび実行します。
\>javac WhereClause.java
\>java WhereClause
上記のコマンドを実行すると、次の出力が表示されます。
101 | Learn C | Yaswanth