数据库多张表链接操作

一.创建数据库test1和test2如下:
test1结构:

这里写图片描述
test1数据:
这里写图片描述

test2结构:
这里写图片描述
test2数据:
这里写图片描述

二.连接操作

1.内连接,两张表通过对应的值进行比较,将相同或者满足一定条件的信息组织起来,有可能一张表中的一条数据连接另一张表中的多条数据。

select * from test1 t1,test2 t2 WHERE t1."id"=t2.test1;

select * from test1 t1 INNER JOIN test2 t2 ON t1."id"=t2.test1;

上面两条语句查询的结果如下:
这里写图片描述

2.左外连接,两张表按照左边表中的数据为准连接,左边表中的一条数据可能对应左边表中的多条数据,如果右边表中没有满足条件的数据,左边表中对应右边的数据以null代替。

select * from test1  t1 LEFT JOIN test2 t2 ON t1."id"=t2.test1;

select * from test1  t1 LEFT OUTER JOIN test2 t2 ON t1."id"=t2.test1;

上面两条查询结果一致:
这里写图片描述

3.右外连接,两张表按照右边表中的数据为准连接,右边表中的一条数据可能对应左边表中的多条数据,如果左边表中没有满足条件的数据,右边表中对应左边的数据以null代替。

select * from test1 t1 RIGHT JOIN test2 t2 ON t1."id"=t2.test1;

select * from test1 t1 RIGHT OUTER JOIN test2 t2 ON t1."id"=t2.test1;

上面两条查询结果一致:
这里写图片描述

4.全链接,按照左右表中的所有数据为准进行连接,如果表中一张表中没有找到另一张表中的数据以null代替。

select * from test1 t1 FULL OUTER JOIN test2 t2 on t1."id"=t2.test1;

查询结果:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java导入Excel数据库张表中的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Iterator; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelImporter { public static void main(String[] args) { String excelFilePath = "path/to/excel/file.xlsx"; try { FileInputStream inputStream = new FileInputStream(new File(excelFilePath)); Workbook workbook = new XSSFWorkbook(inputStream); Iterator<Sheet> sheetIterator = workbook.iterator(); while (sheetIterator.hasNext()) { Sheet sheet = sheetIterator.next(); String tableName = sheet.getSheetName(); Iterator<Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); // 获取数据库连接 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database", "username", "password"); // 构建插入语句 StringBuilder sql = new StringBuilder(); sql.append("INSERT INTO ").append(tableName).append(" VALUES ("); for (int i = 0; i < row.getLastCellNum(); i++) { sql.append("?,"); } sql.deleteCharAt(sql.length() - 1); sql.append(")"); // 创建预编译语句 PreparedStatement statement = connection.prepareStatement(sql.toString()); int cellIndex = 0; while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); statement.setString(cellIndex + 1, cell.getStringCellValue()); cellIndex++; } // 执行插入语句 statement.executeUpdate(); // 关闭连接和语句 statement.close(); connection.close(); } } workbook.close(); inputStream.close(); System.out.println("Excel导入到数据库成功!"); } catch (Exception e) { e.printStackTrace(); } } } ``` 请注意,上述代码中的数据库连接信息需要根据实际情况进行修改。此外,还需要添加Apache POI和MySQL驱动的依赖。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值