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

クエリに存在しない列を追加しますか?


ASキーワードを使用して、クエリに存在しない列を追加します。構文は次のとおりです-

SELECT yourColumnName1,yourColumnName2,....N,yourValue AS yourColumnName,....N' FROM yourTableName;

上記の構文を理解するために、テーブルを作成しましょう。テーブルを作成するためのクエリは次のとおりです-

mysql> create table ColumnDoesNotExists
    -> (
    -> UserId int,
    -> UserName varchar(20)
    -> );
Query OK, 0 rows affected (0.67 sec)

挿入コマンドを使用して、テーブルにいくつかのレコードを挿入します。クエリは次のとおりです-

mysql> insert into ColumnDoesNotExists(UserId,UserName) values(100,'Larry');
Query OK, 1 row affected (0.14 sec)
mysql> insert into ColumnDoesNotExists(UserId,UserName) values(101,'Sam');
Query OK, 1 row affected (0.22 sec)
mysql> insert into ColumnDoesNotExists(UserId,UserName) values(102,'Mike');
Query OK, 1 row affected (0.15 sec)
mysql> insert into ColumnDoesNotExists(UserId,UserName) values(103,'David');
Query OK, 1 row affected (0.15 sec)
mysql> insert into ColumnDoesNotExists(UserId,UserName) values(104,'Robert');
Query OK, 1 row affected (0.10 sec)
mysql> insert into ColumnDoesNotExists(UserId,UserName) values(105,'Maxwell');
Query OK, 1 row affected (0.20 sec)
mysql> insert into ColumnDoesNotExists(UserId,UserName) values(106,'Bob');
Query OK, 1 row affected (0.17 sec)
mysql> insert into ColumnDoesNotExists(UserId,UserName) values(107,'John');
Query OK, 1 row affected (0.17 sec)
mysql> insert into ColumnDoesNotExists(UserId,UserName) values(108,'James');
Query OK, 1 row affected (0.18 sec)

selectステートメントを使用して、テーブルのすべてのレコードを表示します。クエリは次のとおりです-

mysql> select *from ColumnDoesNotExists;

出力

+--------+----------+
| UserId | UserName |
+--------+----------+
|    100 |    Larry |
|    101 |      Sam |
|    102 |     Mike |
|    103 |    David |
|    104 |   Robert |
|    105 |  Maxwell |
|    106 |      Bob |
|    107 |     John |
|    108 |    James |
+--------+----------+
9 rows in set (0.00 sec)

クエリに存在しない列名を追加するためのクエリは次のとおりです-

mysql> select UserId,UserName,23 AS Age from ColumnDoesNotExists;

出力

+--------+----------+-----+
| UserId | UserName | Age |
+--------+----------+-----+
|    100 |    Larry |  23 |
|    101 |      Sam |  23 |
|    102 |     Mike |  23 |
|    103 |    David |  23 |
|    104 |   Robert |  23 |
|    105 |  Maxwell |  23 |
|    106 |      Bob |  23 |
|    107 |     John |  23 |
|    108 |    James |  23 |
+--------+----------+-----+
9 rows in set (0.00 sec)

  1. 列の値を置き換えるMySQLクエリ

    まずテーブルを作成しましょう- mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Score int ); Query OK, 0 rows affected (0.45 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入します mysql> insert into DemoTable(Score) values(56); Query OK, 1 row affected (0.24 sec)

  2. 複数の行が存在するかどうかを確認するMySQLクエリ?

    まずテーブルを作成しましょう- mysql> create table DemoTable1219 (    Id int,    Name varchar(40) ); Query OK, 0 rows affected (0.43 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入します mysql> insert into DemoTable1219 values(100,'Adam'); Query OK, 1 row affected (0.11 sec) mysql> insert in