赞
踩
一、group by使用问题记录
问题:使用如下查询
SELECT id,count(*) from stu GROUP BY grade; 报错:1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.stu.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
原因:当使用GROUP BY
语句时,SELECT
列表中的所有列都必须是聚合函数的参数或者是GROUP BY
语句中列出的列
二、子查询 sql语句
语句:
SELECT cust_name,cust_state,(SELECT COUNT(*) FROM orders WHERE orders.cust_id=customers.cust_id) AS orders FROM customers ORDER BY cust_name;
解释:表示对于customers表的每一行都执行一次这个子查询
SELECT COUNT(*) FROM orders WHERE orders.cust_id=customers.cust_id)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。