首先,您需要知道一个表中有多少列。以下是了解列名的语法-
show columns from yourTableName;
以下是连接所有列的语法-
select concat(yourColumnName1,yourColumnName2,yourColumnName3,........N) from yourTableName;
让我们首先创建一个表-
mysql> create table DemoTable ( CustomerId int, CustomerName varchar(20), CustomerAge int );
以下是查询以了解确切的列-
mysql> show columns from DemoTable;
这将产生以下输出-
+--------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+-------------+------+-----+---------+-------+ | CustomerId | int(11) | YES | | NULL | | | CustomerName | varchar(20) | YES | | NULL | | | CustomerAge | int(11) | YES | | NULL | | +--------------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
使用插入命令在表中插入记录-
mysql> insert into DemoTable values(101,'Chris',26); mysql> insert into DemoTable values(102,'Robert',27);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable;
这将产生以下输出-
+------------+--------------+-------------+ | CustomerId | CustomerName | CustomerAge | +------------+--------------+-------------+ | 101 | Chris | 26 | | 102 | Robert | 27 | +------------+--------------+-------------+ 2 rows in set (0.00 sec)
以下是连接所有列的查询-
mysql> select concat(CustomerId,CustomerName,CustomerAge) from DemoTable;
这将产生以下输出-
+---------------------------------------------+ | concat(CustomerId,CustomerName,CustomerAge) | +---------------------------------------------+ | 101Chris26 | | 102Robert27 | +---------------------------------------------+ 2 rows in set (0.00 sec)