MySQL
 Computer >> コンピューター >  >> プログラミング >> MySQL

AND&OR演算子を使用して複数の行レコードを返すMySQLクエリ


まずテーブルを作成しましょう-

mysql> create table DemoTable
(
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentName varchar(40),
   StudentMathMarks int,
   StudentMySQLMarks int,
   status ENUM('ACTIVE','INACTIVE')
);
Query OK, 0 rows affected (0.47 sec)

挿入コマンド-

を使用して、テーブルにいくつかのレコードを挿入します
mysql> insert into DemoTable(StudentName,StudentMathMarks,StudentMySQLMarks,status) values('Chris',45,67,'active');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable(StudentName,StudentMathMarks,StudentMySQLMarks,status) values('Bob',89,78,'inactive');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable(StudentName,StudentMathMarks,StudentMySQLMarks,status) values('David',56,68,'active');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable(StudentName,StudentMathMarks,StudentMySQLMarks,status) values('Robert',68,75,'active');
Query OK, 1 row affected (0.18 sec)

selectステートメントを使用してテーブルのすべてのレコードを表示する-

mysql> select *from DemoTable;

これにより、次の出力が生成されます-

+-----------+-------------+------------------+-------------------+----------+
| StudentId | StudentName | StudentMathMarks | StudentMySQLMarks | status   |
+-----------+-------------+------------------+-------------------+----------+
|         1 | Chris       |               45 |                67 | ACTIVE   |
|         2 | Bob         |               89 |                78 | INACTIVE |
|         3 | David       |               56 |                68 | ACTIVE   |
|         4 | Robert      |               68 |                75 | ACTIVE   |
+-----------+-------------+------------------+-------------------+----------+
4 rows in set (0.00 sec)

以下は、AND&OR演算子-

を使用して複数の行レコードを返すクエリです。
mysql> select *from DemoTable
   where status='active'
and (StudentMathMarks=68 or StudentMySQLMarks=67);

これにより、次の出力が生成されます-

+-----------+-------------+------------------+-------------------+--------+
| StudentId | StudentName | StudentMathMarks | StudentMySQLMarks | status |
+-----------+-------------+------------------+-------------------+--------+
|         1 | Chris       |               45 |                67 | ACTIVE |
|         4 | Robert      |               68 |                75 | ACTIVE |
+-----------+-------------+------------------+-------------------+--------+
2 rows in set (0.00 sec)

  1. 複数のレコードをすばやく挿入するMySQLクエリ

    複数のレコードをすばやく挿入するには、単一のINSERTを使用して、次の構文に従います- insert into yourTableName values(yourValue1,yourValue2,...N),(yourValue1,yourValue2,...N).....N; 上記の構文を理解するために、テーブルを作成しましょう- mysql> create table DemoTable2007 (    Amount1 int,    Amount2 int,    Amount3 int ); Query OK,

  2. AND&OR演算子を使用した単一のMySQLクエリを使用して特定のレコードをフェッチします

    まずテーブルを作成しましょう- mysql> create table DemoTable2015    -> (    -> StudentId int,    -> StudentName varchar(20),    -> StudentCountryName varchar(20) -> ); Query OK, 0 rows affected (1.20 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入します mysql> inse