默认使用 SELECT 语句: 当加上in范围后,结果如下图: in范围内的数据,如果有重复的,只会选择第一个数据. 所以如果不是直接使用SQL语句来查询,而是在代码中来查询时,记得使用 distinct 关键字 如: select id, name from table1 where ref_id in ( select id from table2 ) 其实是相当于: select id, name from table1 where ref_id in ( select distinct
写sql的思路不同于常规编程语言(C.python)等等.前者,考虑如何一步步地得到最终答案:后者,考虑如何一步步地收缩数据范围. 简而言之,前者是面向过程化(for each row do x),后者是面向集合(do all -> another all). 具体来讲,写sql从两种角度考虑: 一.集合 首先考虑,要得到正确结果,需要把哪些表,以何种形式连接起来: 对连接之后的大集合,做怎么样的过滤 (where.having) 根据需要做出合适的投影 (select) 可能需要重复前面三步
-- 首先,以超级管理员的身份登录oracle sqlplus sys/bjsxt as sysdba --然后,解除对scott用户的锁 alter user scott account unlock; --那么这个用户名就能使用了. --(默认全局数据库名orcl) 1.select ename, sal * 12 from emp; --计算年薪 2.select 2*3 from dual; --计算一个比较纯的数据用dual表 3.select sysdate from dual; -
1.查询语句的基本操作 - select - from - where - group by - having - distinct - order by - limit - 聚合函数: count, max, min, avg, sum 2.单表查询: #前期表与数据准备 # 创建一张部门表 create table emp( id int not null unique auto_increment, name ) not null, sex enum('male','female') no