1.1 where子句

1 select location "where is waldo?" 
2 from puzzle
3 where name ='waldo';

在where子句中使用列name,但select中没有提及。

MySQL允许:where子句使用select中未出现的列。

location 关键字代表一个未知的列名。

 

1.2 order by子句

1.2.1按照单一值排序

默认是升序ASC,一般不写,这里排降序DESC

1 select *
2 from checks
3 order by check DESC

1.2.2按照多个值排序(多个字段)

1 select *
2 from checks
3 group by payee ASC ,remarks DESC;

*注意 :这里的payee和remarks有先后。是先按照支付人的字母序排列,再安装备注的字母序倒序排列。

1 select payee, amount,remarks
2 from checks
3 order by check#;

MySQL允许:order by 子句使用 select中未出现的列。

 

1.3 group by子句

1.3.1单独使用group by

 

 1 create table checks
 2 (`check`          integer       not null,
 3  payee          varchar(20)      not null,
 4  amount         decimal(6,2)     not null,
 5  remarks        varchar(20)     not null);
 6 
 7 insert into checks values
 8 ('1', 'Ma Bell', '150', 'Have sons next time'),
 9 ('2', 'Reading R.R.', '245.34', 'Train to Chicago'),
10 ('3', 'Ma Bell', '200.32', 'Celluar Phone'),
11 ('4', 'Local Utilities', '98', 'Gas'),
12 ('5', 'Joes Stale $ Dent', '150', 'Groceries'),
13 ('6', 'Cash', '25', 'Wild Night Out'),
14 ('7', 'Joans Gas', '25.1', 'Gas');
1 select payee from checks
2 group by amount;

列出支付了不同钱数的支付人。(错误的!!!

group by 使用了未在select中出现的列。

实际上,select子句中的列必须出现在group by子句中。

不要在select中使用既不聚合也不在group by子句中的列。

 

1.3.2聚合函数与group by子句

 

 

posted on 2018-05-05 21:32  竹马真心  阅读(239)  评论(0编辑  收藏  举报