让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, ClientName varchar(100), ClientAge int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(ClientName,ClientAge) values('Robert',45); mysql> insert into DemoTable(ClientName,ClientAge) values('Mike',55); mysql> insert into DemoTable(ClientName,ClientAge) values('Bob',42); mysql> insert into DemoTable(ClientName,ClientAge) values('Sam',47);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----+------------+-----------+ | Id | ClientName | ClientAge | +----+------------+-----------+ | 1 | Robert | 45 | | 2 | Mike | 55 | | 3 | Bob | 42 | | 4 | Sam | 47 | +----+------------+-----------+ 4 rows in set (0.00 sec)
以下是从MySQL数据库中选择随机条目的查询-
mysql> select *from DemoTable order by rand() limit 1;
这将产生以下输出-
+----+------------+-----------+ | Id | ClientName | ClientAge | +----+------------+-----------+ | 3 | Bob | 42 | +----+------------+-----------+ 1 row in set (0.00 sec)