MySQL SELECTのフィールドを連結しますか?
MySQL SELECTでフィールドを連結するには、次の構文に示すようにCONCAT()関数を使用します。
SELECT CONCAT(yourColumnName1,’anyConcatenationString’),CONCAT(yourColumnName2,’anyC oncatenationString’),....N from yourTableName;
上記の構文を理解するために、最初にテーブルを作成しましょう。テーブルを作成するためのクエリは次のとおりです。
mysql> create table selectConcat -> ( -> StudentId int, -> StudentName varchar(100), -> StudentAge int -> ); Query OK, 0 rows affected (1.32 sec)
挿入コマンドを使用して、テーブルにいくつかのレコードを挿入します。クエリは次のとおりです-
mysql> insert into selectConcat values(1,'Carol',23); Query OK, 1 row affected (0.19 sec) mysql> insert into selectConcat values(2,'John',24); Query OK, 1 row affected (0.25 sec) mysql> insert into selectConcat values(3,'Adam',25); Query OK, 1 row affected (0.14 sec) mysql> insert into selectConcat values(4,'Bob',21); Query OK, 1 row affected (0.20 sec) mysql> insert into selectConcat values(5,'Sam',22); Query OK, 1 row affected (0.16 sec)
selectステートメントを使用して、テーブルのすべてのレコードを表示します。クエリは次のとおりです-
mysql> select *from selectConcat;
以下は出力です。
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 1 | Carol | 23 | | 2 | John | 24 | | 3 | Adam | 25 | | 4 | Bob | 21 | | 5 | Sam | 22 | +-----------+-------------+------------+ 5 rows in set (0.00 sec)
selectステートメントのフィールドに連絡してください。クエリは次のとおりです。
mysql> select concat(StudentId,' as an Id'),concat(StudentName,' as a Name') from selectConcat;
以下は出力です。
+-------------------------------+----------------------------------+ | concat(StudentId,' as an Id') | concat(StudentName,' as a Name') | +-------------------------------+----------------------------------+ | 1 as an Id | Carol as a Name | | 2 as an Id | John as a Name | | 3 as an Id | Adam as a Name | | 4 as an Id | Bob as a Name | | 5 as an Id | Sam as a Name | +-------------------------------+----------------------------------+ 5 rows in set (0.00 sec)
-
Idによって区別されるconcatをグループ化するMySQLクエリ?
まずテーブルを作成しましょう- mysql> create table DemoTable ( Id int, Name varchar(100) ); Query OK, 0 rows affected (0.64 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入します mysql> insert into DemoTable values(100,'Chris'); Query OK, 1 row affected (0.44 sec) mysql> insert into Dem
-
MySQLでSelectクエリを使用して挿入
SELECTクエリを使用した挿入の場合、構文は次のとおりです- insert into yourTableName(yourColumnName1,yourColumnName2,yourColumnName3,...N) select yourValue1,yourValue2,yourValue3,......N;を選択します。 まずテーブルを作成しましょう- mysql> create table DemoTable1603 -> ( -> StudentId int, -> Stud