MySQL为表和字段取别名 (AS关键字)

1 为表取别名

mysql> select * from students;
+----+--------+-------+------+------+
| id | number | name  | sex  | age  |
+----+--------+-------+------+------+
|  1 |   1111 | LiLy  | W    |   12 |
|  2 |   1112 | Lucy  | W    |   11 |
|  3 |   1113 | LiLei | M    |   13 |
|  4 |   1117 | Bird  | NULL |   33 |
+----+--------+-------+------+------+
4 rows in set (0.00 sec)

mysql> select s.name from students as s where age>12;
+-------+
| name  |
+-------+
| LiLei |
| Bird  |
+-------+
2 rows in set (0.00 sec)

2 为字段取别名

mysql> select name as student from students where age<20;
+---------+
| student |
+---------+
| LiLy    |
| Lucy    |
| LiLei   |
+---------+
3 rows in set (0.00 sec)

 


版权声明:本文为u012868077原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。