sql批量更新加排除 mysql java 更新数据库字段number,一到十的数据。并且排除掉number为3和4的。只更新1 2 5 6 7 8 9 10这种sql应该怎么写
时间: 2023-03-13 17:00:48 浏览: 88
可以使用下面的SQL语句:
UPDATE table_name SET number = CASE
WHEN number = 1 THEN 1
WHEN number = 2 THEN 2
WHEN number = 5 THEN 5
WHEN number = 6 THEN 6
WHEN number = 7 THEN 7
WHEN number = 8 THEN 8
WHEN number = 9 THEN 9
WHEN number = 10 THEN 10
ELSE number
END
WHERE number NOT IN (3,4);
相关问题
数据库每条数据有商户id字段、数量number字段、时间createtime字段 现在要查询近七日每个商户的number总数 根据日期分组 mysql和java实现代码
MySQL查询语句如下:
```
SELECT
merchant_id,
DATE_FORMAT(createtime, '%Y-%m-%d') as date,
SUM(number) as total_number
FROM
your_table_name
WHERE
createtime >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)
GROUP BY
merchant_id, date;
```
上面的查询语句会返回每个商户在过去七天内每天的number总数。
Java代码实现如下:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class TotalNumberByMerchant {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/your_database_name";
String user = "your_username";
String password = "your_password";
try (Connection conn = DriverManager.getConnection(url, user, password)) {
String sql = "SELECT merchant_id, DATE_FORMAT(createtime, '%Y-%m-%d') as date, SUM(number) as total_number "
+ "FROM your_table_name "
+ "WHERE createtime >= ? "
+ "GROUP BY merchant_id, date";
PreparedStatement pstmt = conn.prepareStatement(sql);
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date today = new Date();
cal.setTime(today);
cal.add(Calendar.DATE, -7);
Date lastWeek = cal.getTime();
pstmt.setString(1, sdf.format(lastWeek));
ResultSet rs = pstmt.executeQuery();
List<TotalNumberByMerchantDTO> list = new ArrayList<>();
while (rs.next()) {
TotalNumberByMerchantDTO dto = new TotalNumberByMerchantDTO();
dto.setMerchantId(rs.getInt("merchant_id"));
dto.setDate(rs.getString("date"));
dto.setTotalNumber(rs.getInt("total_number"));
list.add(dto);
}
System.out.println(list);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
class TotalNumberByMerchantDTO {
private int merchantId;
private String date;
private int totalNumber;
public int getMerchantId() {
return merchantId;
}
public void setMerchantId(int merchantId) {
this.merchantId = merchantId;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public int getTotalNumber() {
return totalNumber;
}
public void setTotalNumber(int totalNumber) {
this.totalNumber = totalNumber;
}
@Override
public String toString() {
return "TotalNumberByMerchantDTO [merchantId=" + merchantId + ", date=" + date + ", totalNumber=" + totalNumber
+ "]";
}
}
```
上面的Java代码会连接到MySQL数据库并执行上述查询语句,将结果封装成DTO并输出到控制台。请将代码中的`your_database_name`、`your_username`、`your_password`、`your_table_name`替换为实际的数据库名、用户名、密码和表名。
Mysql加字段和长度sql
在MySQL中,如果你想向已经存在的表中添加新的字段并指定其长度,你可以使用ALTER TABLE语句。这是一个基本的例子:
```sql
ALTER TABLE table_name ADD COLUMN new_column_name data_type(length);
```
这里,`table_name`是你想要修改的表名,`new_column_name`是新字段的名字,`data_type`是字段的数据类型,例如`VARCHAR`, `INT`, `DATE`等,而`length`(对于字符串类型)则是该字段允许的最大字符数。
举个例子,假设你想在一个名为`users`的表中添加一个新的`email`字段,长度为100个字符,可以这样写:
```sql
ALTER TABLE users ADD COLUMN email VARCHAR(100);
```
如果你已经有了默认值,并希望给字段设置非空约束,可以加上`NOT NULL`条件:
```sql
ALTER TABLE users ADD COLUMN phone_number INT NOT NULL;
```
阅读全文