Mysql
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Mysql

CHÈN ... CHỌN ... Ở ĐÂU ... TRÊN DUPLICATE ... truy vấn mysql

Tôi không nghĩ rằng bạn có thể sử dụng các hàm tổng hợp trong ON DUPLICATE . MySQL thấy kiểu SQL của bạn như thế này:

insert into report_count_assets
expr
on duplicate key update
...

ON DUPLICATE không biết chuyện gì đang xảy ra trong expr , nó chỉ biết rằng nó có một hàng trùng lặp duy nhất cần xử lý. Mà không biết chuyện gì đang xảy ra bên trong expr , không có ngữ cảnh cho count s để hoạt động. Ngoài ra, bạn phải sử dụng các giá trị values trong CẬP NHẬT:

Và các giá trị values(count(x)) không phải là cú pháp hợp lệ. Nhưng các giá trị values(column_name) là hợp lệ nên điều này sẽ hoạt động:

INSERT INTO report_count_assets
(product_id, asset_count, asset_type_image, asset_type_video, asset_type_sound, asset_type_install)
    SELECT products.product_id, 
    count(product_assets.asset_id),
    count(case when assets.asset_type_id=1 THEN 1 END), 
    count(case when assets.asset_type_id=2 THEN 1 END), 
    count(case when assets.asset_type_id=3 THEN 1 END), 
    count(case when assets.asset_type_id=11 THEN 1 END) 
    FROM products 
    LEFT JOIN product_assets USING (product_id) 
    LEFT JOIN assets USING (asset_id)
    WHERE products.brand_id=671
ON DUPLICATE KEY UPDATE
    asset_count = values(asset_count),
    asset_type_image = values(asset_type_image), 
    asset_type_video = values(asset_type_video), 
    asset_type_sound = values(asset_type_sound), 
    asset_type_install = values(asset_type_install);

Tôi phải đoán tên của product_id trong report_count_assets .

Nếu điều đó không hiệu quả (dường như là không), thì bạn có thể làm điều đó một cách khó khăn bằng cách tính toán trước SELECT. Tạo bảng tạm thời:

create temporary table t (
    product_id int,
    product_count int,
    assets1 int,
    assets2 int,
    assets3 int,
    assets11 int
)

Điền vào nó:

INSERT INTO t (product_id, product_count, assets1, assets2, assets3, assets11)
SELECT products.product_id, 
count(product_assets.asset_id),
count(case when assets.asset_type_id=1 THEN 1 END), 
count(case when assets.asset_type_id=2 THEN 1 END), 
count(case when assets.asset_type_id=3 THEN 1 END), 
count(case when assets.asset_type_id=11 THEN 1 END) 
FROM products 
LEFT JOIN product_assets USING (product_id) 
LEFT JOIN assets USING (asset_id)
WHERE products.brand_id=671

Và sau đó sử dụng bảng tạm thời đó để thực hiện chèn mà bạn thực sự muốn thực hiện:

insert into report_count_assets
    select product_id, product_count, assets1, assets2, assets3, assets11
    from t
on duplicate key update
    asset_count = values(product_count),
    asset_type_image = values(assets1),
    asset_type_video = values(assets2),
    asset_type_sound = values(assets3),
    asset_type_install = values(assets11)


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. kết nối mysql từ một miền khác

  2. Làm thế nào để thoát khỏi mysql trong magento?

  3. Lưu trữ thẻ trong cơ sở dữ liệu. Lưu trữ thẻ một lần hay nhiều lần?

  4. Làm thế nào để loại bỏ các ký tự xấu không phù hợp với mã hóa utf8 trong MySQL?

  5. truy vấn mã zip địa lý