执行sql脚本文件
1、在shell下:mysql -t -h localhost -u root employees < test.sql
-t让输出与在交互模式下输出一致,比较好看, -h指定主机, -u指定用户
2、在交互模式下执行:source test.sql或\. test.sql
3、选出薪水为薪水最大值的员工的员工编号:
select * from salaries where salary=(select max(salary) from salaries);
4、选出每个员工的最大薪水值(因为可能在不同的时间薪水不一样)
select emp_no, max(salary) from salaries group by emp_no;
5、使用自定义的变量:
SELECT @min_salary := MIN(salary), @max_salary := MAX(salary) FROM salaries;
SELECT * FROM salaries WHERE salary=min_salary OR salary=max_salary;
6、OR和UNION
SELECT * FROM salaries WHERE salary=(SELECT MIN(salary) FROM salaries) OR salary=(SELECT MAX(salary) FROM salaries);
SELECT * FROM salaries WHERE salary=(SELECT MIN(salary) FROM salaries) UNION SELECT * FROM salaries WHERE salary=(SELECT MAX(salary) FROM salaries);
在我的机器上OR用了2.62s,UNION用了3.43s,存储引擎是InnoDB