Mysql 使用一个SQL查询更新表

Mysql 使用一个SQL查询更新表,mysql,sql,Mysql,Sql,我有一个主产品表: @Table(name = "product") public class Product implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", unique = true, updatable = false, nullable = false) pr

我有一个主
产品
表:

@Table(name = "product")
public class Product implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, updatable = false, nullable = false)
    private int id;

    @Column(name = "user_id", length = 20)
    private Integer userId;

    @Column(name = "title", length = 75)
    private String title;

    @Column(name = "meta_title", length = 100)
    private String metaTitle;

    @Column(name = "status", length = 100)
    private String status;
}
用于存储应作为列表返回的类别的附加表:

@Table(name = "product_category")
public class ProductCategory implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, updatable = false, nullable = false)
    private int id;

    @Column(name = "product_id", length = 4)
    private Integer productId;

    @Column(name = "category_id", length = 20)
    private Integer categoryId;

}
@Table(name = "product_payment_methods")
public class ProductPaymentMethods implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, updatable = false, nullable = false)
    private int id;

    @Column(name = "product_id", length = 20)
    private Integer productId;

    @Column(name = "payment_methods", length = 20000)
    private String paymentMethods;
}
存储应作为列表返回的付款方式的附加表:

@Table(name = "product_category")
public class ProductCategory implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, updatable = false, nullable = false)
    private int id;

    @Column(name = "product_id", length = 4)
    private Integer productId;

    @Column(name = "category_id", length = 20)
    private Integer categoryId;

}
@Table(name = "product_payment_methods")
public class ProductPaymentMethods implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, updatable = false, nullable = false)
    private int id;

    @Column(name = "product_id", length = 20)
    private Integer productId;

    @Column(name = "payment_methods", length = 20000)
    private String paymentMethods;
}
SQL查询:

SELECT *
FROM Product
INNER JOIN product_category ON Product.id = product_category.productId
INNER JOIN product_payment_methods ON Product.id = product_payment_methods.productId
WHERE userId = 1
我使用此DTO更新数据:

public class ProductFullDTO {

    private int id;

    private Integer userId;

    private List<Integer> categories;

    private List<String> paymentMethods;
}
公共类ProductFullDTO{
私有int-id;
私有整数用户标识;
私人名单类别;
私人清单支付方式;
}
如何使用一个SQL更新查询将数据更新到表中

UPDATE Product p
INNER JOIN product_category pc ON p.id = pc.productId
INNER JOIN product_payment_methods ppm ON p.id = ppm.productId
SET p.a = 'something',
    pc.b = 42,
    ppm.c = pc.c
WHERE userId = 1
通过选择,您可以确定哪些行将受到影响

SELECT *
FROM Product
INNER JOIN product_category ON Product.id = product_category.productId
INNER JOIN product_payment_methods ON Product.id = product_payment_methods.productId
WHERE userId = 1
此查询中的所有行都将成为写入更新查询中的数据

SET p.a = 'something',
    pc.b = 42,
    ppm.c = pc.c
如您所见,您可以组合来自不同表的数据,并使用结果集中的行中的数据更新任何列


更新查询应始终在测试环境中进行测试。

更新是什么意思?这包括添加或删除类别或付款方式吗?我使用的是Spring数据-HQL。是否可以使用HQL实现更新查询?是的,请参阅中的第6章,但我个人没有这样做。