免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3154 | 回复: 3

mysql新手求助,谢谢大家! [复制链接]

论坛徽章:
2
程序设计版块每日发帖之星
日期:2015-09-07 06:20:00程序设计版块每日发帖之星
日期:2015-09-07 06:20:00
发表于 2015-11-16 15:29 |显示全部楼层
[11/16/2015 3:28 PM] Henry Zheng C:
mysql> select * from Customers;
mysql> select * from Customers;
+------------+---------------+----------------------+-----------+------------+----------+--------------+--------------------+-----------------------+
| cust_id    | cust_name     | cust_address         | cust_city | cust_state | cust_zip | cust_country | cust_contact       | cust_email            |
+------------+---------------+----------------------+-----------+------------+----------+--------------+--------------------+-----------------------+
| 1000000001 | Village Toys  | 200 Maple Lane       | Detroit   | MI         | 44444    | USA          | John Smith         | sales@villagetoys.com |
| 1000000002 | Kids Place    | 333 South Lake Drive | Columbus  | OH         | 43333    | USA          | Michelle Green     | NULL                  |
| 1000000003 | Fun4All       | 1 Sunny Place        | Muncie    | IN         | 42222    | USA          | Jim Jones          | jjones@fun4all.com    |
| 1000000004 | Fun4All       | 829 Riverside Drive  | Phoenix   | AZ         | 88888    | USA          | Denise L. Stephens | dstephens@fun4all.com |
| 1000000005 | The Toy Store | 4545 53rd Street     | Chicago   | IL         | 54545    | USA          | Kim Howard         | NULL                  |
+------------+---------------+----------------------+-----------+------------+----------+--------------+--------------------+-----------------------+
5 rows in set (0.00 sec)

mysql> select * from Orders;
+-----------+---------------------+------------+
| order_num | order_date          | cust_id    |
+-----------+---------------------+------------+
|     20005 | 2004-05-01 00:00:00 | 1000000001 |
|     20006 | 2004-01-12 00:00:00 | 1000000003 |
|     20007 | 2004-01-30 00:00:00 | 1000000004 |
|     20008 | 2004-02-03 00:00:00 | 1000000005 |
|     20009 | 2004-02-08 00:00:00 | 1000000001 |
+-----------+---------------------+------------+
5 rows in set (0.00 sec)

mysql> select cust_name,cust_state,(select count(*) from Orders where Orders.cust_id=Customers.cust_id) as orders  from Customers;
+---------------+------------+--------+
| cust_name     | cust_state | orders |
+---------------+------------+--------+
| Village Toys  | MI         |      2 |
| Kids Place    | OH         |      0 |
| Fun4All       | IN         |      1 |
| Fun4All       | AZ         |      1 |
| The Toy Store | IL         |      1 |
+---------------+------------+--------+
5 rows in set (0.00 sec)

我想问下,上面的select语句的原理是什么呢?我感觉是在搜索每一行的时候,都会去执行那个圆括号里的子查询。那我想问的是,程序怎么确定Customers.cust_id的值的呢?

求职 : Linux运维
论坛徽章:
203
拜羊年徽章
日期:2015-03-03 16:15:432015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:57:092015小元宵徽章
日期:2015-03-06 15:58:182015年亚洲杯之约旦
日期:2015-04-05 20:08:292015年亚洲杯之澳大利亚
日期:2015-04-09 09:25:552015年亚洲杯之约旦
日期:2015-04-10 17:34:102015年亚洲杯之巴勒斯坦
日期:2015-04-10 17:35:342015年亚洲杯之日本
日期:2015-04-16 16:28:552015年亚洲杯纪念徽章
日期:2015-04-27 23:29:17操作系统版块每日发帖之星
日期:2015-06-06 22:20:00操作系统版块每日发帖之星
日期:2015-06-09 22:20:00
发表于 2015-11-16 20:57 |显示全部楼层
cust_id   ??

论坛徽章:
2
程序设计版块每日发帖之星
日期:2015-09-07 06:20:00程序设计版块每日发帖之星
日期:2015-09-07 06:20:00
发表于 2015-11-17 08:35 |显示全部楼层
回复 2# lyhabc
我的问题是不是写的不清楚,没有看懂我的问题啊,如果是的话,你哪里没看懂我再解释下。然后,麻烦你帮我解答下呗,谢谢了!

   

论坛徽章:
7
数据库技术版块每日发帖之星
日期:2015-11-17 06:20:00数据库技术版块每日发帖之星
日期:2015-11-18 06:20:00数据库技术版块每日发帖之星
日期:2015-11-30 06:20:00数据库技术版块每月发帖之星
日期:2016-01-07 23:03:06数据库技术版块每周发帖之星
日期:2016-01-07 23:06:31数据库技术版块每周发帖之星
日期:2016-01-07 23:06:47数据库技术版块每日发帖之星
日期:2016-01-14 06:20:00
发表于 2015-11-17 11:21 |显示全部楼层
select cust_name,cust_state,(select count(*) from Orders where Orders.cust_id=Customers.cust_id) as orders  from Customers;

SQL首先执行 from Customers,取出所有的记录,然后逐行去取 cust_name,cust_state和Customers.cust_id,对于 cust_name,cust_state的结果直接返回,但取到Customers.cust_id,必须先执行一个子查询,也就是select count(*) from Orders where Orders.cust_id=Customers.cust_id,将其结果返回回来。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP