慢慢整理,不定时更新
一、 SQL
SQL 常用语句可以参考我之前整理的一篇文章 地址
1、SQL的执行顺序
FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> DISTINCT -> ORDER BY -> TOP
-- 两张表

Rabbit·2020-08-16·707 次阅读
SQL 常用语句可以参考我之前整理的一篇文章 地址
FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> DISTINCT -> ORDER BY -> TOP
-- 两张表
select stuname as 姓名,stuid as 学号,stuaddress as 家庭地址
from stuInfo where stuid in
(select * from stuMarks)
-- 报错
-- Only one expression can be specified in the select list
-- when the subquery is not introduced with EXISTS
-- 解析
-- in (select * from stuMarks) 这里会报一个错误 因为这里用了in,
-- 在in后面select必须只能有一个列组查,
-- 但这里select 后面有 N 个列,所以会报错!
1.1 错误 : 当子查询未与 EXISTS 一起引入时,只能在选择列表中指定一个表达式。
1.2 原因:这里会报一个错误 因为这里用了in, 在in后面select必须只能由一个列组查, 但这里select 后面有 N 个列,所以会报错!
聚集索引:表中存储的数据按照索引的顺序存储,检索效率比普通索引高,但对数据新增/修改/删除的影响比较大。
非聚集索引:不影响表中的数据存储顺序,检索效率比聚集索引低,对数据新增/修改/删除的影响很小。
一张表只有一个聚簇索引,可有多个非聚簇索引。
区别:聚簇索引的顺序就是数据的物理存储顺序,一张表只能有一个聚簇索引 但可以有多个非聚簇索引, 非聚簇索引的索引顺序与数据物理排列顺序无关
适用范围:聚簇索引与非聚簇索引
请给出下面SQL 语句的结果
1) SELECT A.ID, A.Name, B.Age
FROM Table1 as A
INNER JOIN Table2 as B -- INNER JOIN 查询返回的是共同的数据(两张表共同有的数据)
ON A.ID = B.ID
-- LEFT JOIN
2) SELECT A.ID, A.Name, B.Age
FROM Table1 as A --左表 数据全部返回
LEFT JOIN Table2 as B --右表 没有对应返回null
ON A.ID = B.ID
- - 结果
这里可以参考之前的文章 - - 多表查询 地址
1、答案 |:先确定要修改的值,然后根据条件修改具体的值
UPDATE Table2
set Table2.Department='MD' --修改值
where UserId=(select UserId From Table1 where UserName ='Daniel') --判断条件
2) --答案 2
UPDATE B
SET Department ='MD'
FROM B,A
WHERE B.UserId = A.UserId
AND A.UserName ='Daniel'
2、Course 表
-- 请写出删除Title重复记录的SQL语句(保留ID最小的)
DELETE FROM Course
where[ID] NOT IN ( -- not in 尽量不要使用
SELECT MIN(ID) AS ID --MIN 最小的函数
FROM Course
GROUP BY [Title]
)
--1.ID连续的情况:
select * from A where ID between 31 and 40
--2.ID不连续的情况:
--(1).两次对表查询,效率较低。
select top 10 * from A where ID not in (select top 30 ID from A)
--(2).外层查询没有对表A进行查询,效率提高。
select top 10 * from (select top 40 ID from A order by ID) as a order by a.ID desc
--(3).ROW_NUMBER()函数效率更高,SQL2005以上版本可用。
1 select * from(select *,ROW_NUMBER() over(order by ID)as 'userID' from A) as a where a.userID between 31 and 40
2.1 重写:override
1、方法名、参数、返回值相同。 2、子类方法不能缩小父类方法的访问权限。 3、子类方法不能抛出比父类方法更多的异常(但子类方法可以不抛出异常)。 4、存在于父类和子类之间。 5、方法被定义为final不能被重写。
2.2 重载:overload
1、参数类型、个数、顺序至少有一个不相同。 2、不能重载只有返回值不同的方法名。 3、存在于父类和子类、同类中。
2.3 区别 :
// 方法一 递归算法,耗时最长的算法,效率很低.
public class Program
{
static void Main(string[] args)
{
Console.WriteLine(Program.FooNumber(30)); // 直接打点调用
Console.WriteLine(Program.FooNumber2(30)); // 简化后递归
Console.ReadLine();
}
//方法一 递归
public static int FooNumber(int i) // static
{
if (i <= 0)
{
return 0;
}
else if (i > 0 && i <= 2)
{
return 1;
}
else {
return FooNumber(i - 1) + FooNumber(i - 2);
}
}
}
// 简化递归
public static int FooNumber2(int n)
{
if (n <= 0) return 0;
if (n <= 2) return 1;
return checked(FooNumber2(n - 2) + FooNumber2(n - 1));
}
//方法二 循环方法
static void Main(string[] args)
{
int x = 1, y = 1, i = 1;
while (++i < 30)
{
int s = x + y;
x = y;
y = s;
}
Console.WriteLine("第三十个数值是:{0}", y);
Console.ReadKey();
}
-- 执行这个存储过程
create proc proc_score
(
@score int output,
@stuId char(10),
)
as
select @score=score
from score where stuId=@stuId
go
-- 解析
declare @score int --定义输出参数
exec proc_score ‘2011001',@score output
参考答案:使用join()方法,让其他线程等待。使用join的线程会独占执行资源,直到使用完毕,其它线程才能获取执行权。参考博客园 地址
- -计算一个月的具体工作天数 (这里不算什么面试题,只是无意间看到群友讨论这个东西,自己也试了试)
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
DateTime dt = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-01")); // 当前日期月份的第一天
int year = DateTime.Now.Year; // 获得年
int month =DateTime.Now.Month; // 获得月
int days = DateTime.DaysInMonth(year, month); // 获得该月总共多少天
// 休息天数
int weekDays = 0;
for (int i = 0; i < days; i++)
{
// 判断是否为周六,周日,是则记录天数。
switch (dt.DayOfWeek)
{
case DayOfWeek.Saturday:
weekDays++;
break;
case DayOfWeek.Sunday:
weekDays++;
break;
}
dt = dt.AddDays(1);
}
// 工作日
int workDays = days - weekDays;
// this.label1.Text = "工作日:" + workDays.ToString() + "天";
Console.WriteLine("工作日"+workDays.ToString()+"天");
Console.WriteLine("休息日为"+weekDays.ToString()+"天");
Console.ReadKey();
}
}
}
Comments | 1 条评论
不