Home  >  Article  >  Database  >  使用MySQL命令行了解SQL的执行结果

使用MySQL命令行了解SQL的执行结果

WBOY
WBOYOriginal
2016-06-07 16:24:261061browse

在命令行下学习SQL是个非常不错的选择,它可以直观地显示SQL的执行结果,下面使用Navicat的命令行工具看看常用的SQL语句的执行情况。 1. 显示文章id为1的文章标题。 mysql> SELECT title FROM news WHERE id = 1;+-----------------------------------------

在命令行下学习SQL是个非常不错的选择,它可以直观地显示SQL的执行结果,下面使用Navicat的命令行工具看看常用的SQL语句的执行情况。

1. 显示文章id为1的文章标题。

mysql> SELECT title FROM news WHERE id = 1;
+--------------------------------------------+
| title                                 	 |
+--------------------------------------------+
| Java之父:Oracle对Java未来的重要性被夸大了 	 |
+--------------------------------------------+
1 row in set

2. 显示两个字段

mysql> SELECT MAX(id),title FROM news;
+--------------+--------------------------------------------+
| MAX(id) 	   | title                                 		|
+--------------+--------------------------------------------+
|          179 | Java之父:Oracle对Java未来的重要性被夸大了 	|
+--------------+--------------------------------------------+
1 row in set

3. as 的用法

mysql> SELECT MAX(id) as 编号,title as 标题 FROM news;
+------+--------------------------------------------+
| 编号  | 标题                                       |
+------+--------------------------------------------+
|  179 | Java之父:Oracle对Java未来的重要性被夸大了 	|
+------+--------------------------------------------+
1 row in set

4. 显示最后一条新闻

mysql> SELECT id AS ID, title as TITLE FROM news WHERE id = (SELECT MAX(id) FROM news);
+-----+----------------------+
| ID  | TITLE                |
+-----+----------------------+
| 179 | 支付宝会被收归国有? 	 |
+-----+----------------------+
1 row in set

5. 跨表

mysql> SELECT a.title FROM news as a WHERE id = 100;
+-----------------------------+
| title                  	  |
+-----------------------------+
| 文化中国传播收购MSN 50%股份 	  |
+-----------------------------+
1 row in set

6. 跨表

mysql> SELECT a.title, (SELECT b.tag FROM news as b WHERE id = 100) FROM news as a WHERE id = 100;
+-----------------------------+--------------------------------------------------------+
| title                       | (SELECT b.tag FROM news as b WHERE id = 100) |
+-----------------------------+--------------------------------------------------------+
| 文化中国传播收购MSN 50%股份    | MSN                                                    |
+-----------------------------+--------------------------------------------------------+
1 row in set

7. 显示标签与该标签下一共有多少文章

mysql> SELECT tag as 标签, SUM(num) as 数量 FROM news GROUP BY tag;
+----------------+------+
| 标签           | 数量 |
+----------------+------+
| adobe          |    2 |
| android        |   11 |
| apple          |    8 |
| apples         |    1 |
| Chrome         |    2 |
| Firefox        |    2 |
| Foxconn        |    1 |
| google         |   38 |
| Google AdSense |    1 |
| HTML5          |    2 |
| IE6            |    1 |
| intel          |    1 |
| java           |    1 |
| microsoft      |    3 |
| Motorola       |    1 |
| MSN            |    1 |
| oracle         |    3 |
| PHP            |    1 |
| QQ             |    1 |
| Scala          |    1 |
| tencent        |    1 |
| VANCL          |    1 |
| Windows XP     |    1 |
| 乔布斯         |    1 |
| 云计算         |    1 |
| 互联网         |    1 |
| 人人网         |    1 |
| 低碳           |    1 |
| 保罗·艾伦      |    1 |
| 创业           |    1 |
| 创新工场       |    4 |
| 史玉柱         |    1 |
| 唐骏           |    3 |
| 团购           |    1 |
| 域名           |    1 |
| 奇虎360        |    4 |
| 巨人网络       |    1 |
| 年龄歧视       |    2 |
| 广告联盟       |    1 |
| 开心网         |    3 |
| 张朝阳         |    1 |
| 彩虹糖的梦     |    1 |
| 微博           |    1 |
| 微软           |    3 |
| 必应           |    1 |
| 惠普           |    1 |
| 房地产         |    1 |
| 技术类工作     |    1 |
| 招聘           |    1 |
| 探索           |    1 |
| 搜狗           |    1 |
| 搜索           |    1 |
| 支付宝         |    1 |
| 数据中心       |    2 |
| 数据库         |    1 |
| 智力开发       |    1 |
| 暴雪           |    1 |
| 李开复         |    6 |
| 松本行弘       |    1 |
| 比尔·盖茨      |    1 |
| 汽车           |    1 |
| 淘宝网         |    1 |
| 百度           |    1 |
| 硅谷           |    1 |
| 移动开发       |    1 |
| 程序员         |    3 |
| 站长           |    1 |
| 绿坝           |    1 |
| 编程           |    1 |
| 编程语言       |    1 |
| 网游           |    1 |
| 职业规划       |    1 |
| 腾讯           |    5 |
| 腾迅           |    1 |
| 蔡文胜         |    1 |
| 评论           |    3 |
| 诺基亚         |    1 |
| 金山           |    2 |
| 阿里巴巴       |    1 |
| 雷军           |    2 |
| 霍金           |    2 |
| 马云           |    5 |
| 魔兽世界       |    1 |
+----------------+------+
83 rows in set

当然还有很多例子,这里就抛砖引玉下,学习SQL在命令行下确实是个好方法。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn