三个月前刚毕业的时候,听到存储过程就头疼. 写一个SQL存储过程,建立一个表USER 字段是姓名,年龄,职位,权限,然后向里面插入6条数据,然后查询出年龄大于18的所有信息. 下面是答案: 复制代码 --这是建立表以及插入数据 use pubs go create table users( Name varchar(20), Age int, Position varchar(20), quanxian varchar(20) ) Insert into users values('111',1…
一道sql面试题(查询语句) id name age 1 a 11 2 b 11 3 c 12 4 d 13 5 e 12 . . . 查询age唯一的那一个 这个应该怎么写 满意答案 热心问友 2010-10-14 select * from table1 where id not in (select age from table1 group by age having count(1)>1) --Up…
如何用一个SQL查询出一个班级各个学科第N名是谁? 首先贴出建表语句,方便大家本地测试: -- 建表语句 CREATE TABLE score ( id INT NOT NULL auto_increment, `name` VARCHAR (20) NOT NULL DEFAULT '' COMMENT '姓名', sub VARCHAR (20) NOT NULL DEFAULT '' COMMENT '学科', score INT NOT NULL DEFAULT 0 COMMENT '分…
Buffer sort引发的血案 今天遇到的一个问题,在线系统上,有两张表,test1大概50G,test2大概200G,需要查询出来test1表中部分记录,并且这些记录不存在test2表中.于是就写了一个sql: select t1.* from test1 t1, test2 t2 where t1.col1 = t2.col1(+) and t1.col2 = t2.col2(+) and t1.col3 = t2.col3(+) and t2.col1 is null; 因为是在线系统,…
原文:一个sql的优化 目的:为了查询某天某个服务器上的登录id的个数 刚开始编写的sql: select count(a.mac) logusers from Log_MacLogin_All a where isMoNi != 1 and loginTime <= '2015-02-01 23:59:59' and loginTime >= '2015-02-01 00:00:00' …
原文:如何使用OPENQUERY访问另一个SQL Server 在项目中,经常会遇到一个数据库访问另一个数据库,[CNVFERPDB]为服务器名,[CE3]为库名 SELECT Dtl.* FROM CNVFERPDB. CE3.ce3.ZTLE0125 Dtl INNER JOIN CNVFERPDB.CE3.ce3.ZTLE0124 Mst ON Dtl.RECVSUPPNO = Mst.RECVSUPPNO AND Dtl.MANDT = Mst.MANDT ' and Dtl.BRAND…
'; -- table2 的 name 作为 table1的条件 select * from table1 where name in (select name from table2) --如果有多条语句,可以使用字段相加再等于 select * from table1 where fld1+fld2 in (select fld1+fld2 from table2) --也可以使用INNER JOIN 进行查询 select a.* from table1 a inner join (sel…