10-31 sql-sample

本题目要求编写SQL语句,检索出每个班级中分数最低的同学id,姓名,分数,班级名称a111

表结构:

create table tb_student (
    id int not null primary key,
    name varchar(32)
);
create table tb_score (
    stu_id int,
    score int
);
create table tb_class (
    id int not null,
    name varchar(32)
);
create table tb_student_class
(
    id       int null,
    class_id int null,
    stu_id   int null
);

 

表样例

 

 思路:

1. 首先,我们使用两个子查询(子查询b1和子查询b2)来获取学生的学号、姓名、班级名称和分数。在子查询b1中,我们通过连接tb_score(成绩表)、tb_class(班级表)、tb_student_class(学生班级关联表)和tb_student(学生表),获取到学生的学号、姓名、班级名称和分数。在子查询b2中,我们通过连接tb_score、tb_class和tb_student_class,获取到每个班级中的最低分数。

2. 然后,在主查询中,我们使用WHERE子句来筛选出满足条件的学生。我们通过比较班级名称和分数,找出每个班级中成绩最低的学生。

3. 最后,我们在主查询中选择子查询b1中的学号、姓名、班级名称和分数作为结果。

select b1.stu_id,b1.stu_name,b1.class_name,b1.score
from
(
select a3.stu_id,a4.name as stu_name,a2.name as class_name,score
from tb_score as a1,tb_class as a2,tb_student_class as a3,tb_student as a4
where a1.stu_id = a3.stu_id and a3.class_id = a2.id and a4.id = a3.stu_id
) as b1,
(
select name,min(score) as mins
from tb_score as a1,tb_class as a2,tb_student_class as a3
where a1.stu_id = a3.stu_id and a3.class_id = a2.id
group by name
) as b2
where b1.class_name = b2.name and b1.score = b2.mins

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要将Excel数据插入SqlServer并避免重复数据,可以使用以下步骤: 1. 将Excel数据读取到DataTable。 2. 在DataTable检查是否存在重复数据,如果存在则删除重复数据。 3. 使用SqlBulkCopy将数据插入到SqlServer。 以下是示例代码: ```csharp using System.Data; using System.Data.OleDb; using System.Data.SqlClient; //连接Excel文件 string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\sample.xlsx;Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\""; OleDbConnection excelConn = new OleDbConnection(connString); excelConn.Open(); //读取Excel数据到DataTable string sql = "SELECT * FROM [Sheet1$]"; OleDbDataAdapter adapter = new OleDbDataAdapter(sql, excelConn); DataTable excelData = new DataTable(); adapter.Fill(excelData); excelConn.Close(); //检查是否存在重复数据 for (int i = excelData.Rows.Count - 1; i >= 0; i--) { DataRow row = excelData.Rows[i]; if (IsDuplicate(row)) { excelData.Rows.Remove(row); } } //将数据插入到SqlServer string sqlServerConnString = "Data Source=.;Initial Catalog=TestDB;Integrated Security=True;"; SqlConnection sqlServerConn = new SqlConnection(sqlServerConnString); sqlServerConn.Open(); SqlTransaction tran = sqlServerConn.BeginTransaction(); try { using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlServerConn, SqlBulkCopyOptions.Default, tran)) { bulkCopy.DestinationTableName = "Person"; bulkCopy.ColumnMappings.Add("Name", "Name"); bulkCopy.ColumnMappings.Add("Age", "Age"); bulkCopy.WriteToServer(excelData); } tran.Commit(); } catch (Exception ex) { tran.Rollback(); throw ex; } finally { sqlServerConn.Close(); } //检查是否存在重复数据的方法 private bool IsDuplicate(DataRow row) { string name = row["Name"].ToString(); int age = int.Parse(row["Age"].ToString()); string sql = "SELECT COUNT(*) FROM Person WHERE Name=@Name AND Age=@Age"; using (SqlConnection conn = new SqlConnection(sqlServerConnString)) { SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@Name", name); cmd.Parameters.AddWithValue("@Age", age); conn.Open(); int count = (int)cmd.ExecuteScalar(); if (count > 0) { return true; } else { return false; } } } ``` 以上代码使用了IsDuplicate方法来检查是否存在重复数据,该方法查询了目标表是否存在相同的Name和Age值。如果存在则返回true,否则返回false。在使用SqlBulkCopy插入数据时,应该先将Excel数据读取到DataTable,然后在DataTable检查是否存在重复数据,最后再将数据插入到SqlServer

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

THK-J

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值