MySQL - UPDATE查询基于SELECT查询

我需要检查(从同一个表中)两个事件之间是否存在基于日期-时间的关联。

一组数据将包含某些事件的结束日期-时间,另一组数据将包含其他事件的开始日期-时间。

如果第一个事件在第二个事件之前完成,那么我想把它们联系起来。

到目前为止,我有:

SELECT name as name_A, date-time as end_DTS, id as id_A
FROM tableA WHERE criteria = 1




SELECT name as name_B, date-time as start_DTS, id as id_B
FROM tableA WHERE criteria = 2

然后我加入他们:

SELECT name_A, name_B, id_A, id_B,
if(start_DTS > end_DTS,'VALID','') as validation_check
FROM tableA
LEFT JOIN tableB ON name_A = name_B

然后,我可以根据validation_check字段运行一个带有SELECT嵌套的UPDATE查询吗?

1362962 次浏览

实际上你有两种方法:

MySQL更新连接语法:

UPDATE tableA a
INNER JOIN tableB b ON a.name_a = b.name_b
SET validation_check = if(start_dts > end_dts, 'VALID', '')
-- where clause can go here

ANSI SQL语法:

UPDATE tableA SET validation_check =
(SELECT if(start_DTS > end_DTS, 'VALID', '') AS validation_check
FROM tableA
INNER JOIN tableB ON name_A = name_B
WHERE id_A = tableA.id_A)

挑一个对你来说最自然的。

如果有人试图将数据从一个数据库更新到另一个数据库,而不管他们的目标是哪个表,那么必须有一些标准来做到这一点。

这个对所有关卡来说都更好更干净:

UPDATE dbname1.content targetTable


LEFT JOIN dbname2.someothertable sourceTable ON
targetTable.compare_field= sourceTable.compare_field
SET
targetTable.col1  = sourceTable.cola,
targetTable.col2 = sourceTable.colb,
targetTable.col3 = sourceTable.colc,
targetTable.col4 = sourceTable.cold

Traaa !效果很好!

有了以上的理解,您就可以修改设置的字段和“on”标准来完成您的工作。您还可以执行检查,然后将数据拉入临时表,然后使用替换表和列名的上述语法运行更新。

希望有用,如果不行请告诉我。我会为你写一个精确的查询。

UPDATE
`table1` AS `dest`,
(
SELECT
*
FROM
`table2`
WHERE
`id` = x
) AS `src`
SET
`dest`.`col1` = `src`.`col1`
WHERE
`dest`.`id` = x
;

希望这对你有用。

UPDATE
receipt_invoices dest,
(
SELECT
`receipt_id`,
CAST((net * 100) / 112 AS DECIMAL (11, 2)) witoutvat
FROM
receipt
WHERE CAST((net * 100) / 112 AS DECIMAL (11, 2)) != total
AND vat_percentage = 12
) src
SET
dest.price = src.witoutvat,
dest.amount = src.witoutvat
WHERE col_tobefixed = 1
AND dest.`receipt_id` = src.receipt_id ;

希望这将帮助你在一个情况下,你必须匹配和更新两个表。

我在寻找一个非常复杂的连接的解决方案时发现了这个问题。这是另一种解决方案,一个更复杂的问题,我认为可能有用。

我需要填充活动表中的product_id字段,其中活动以单位编号,单位以级别编号(使用字符串??N标识),这样就可以使用SKU(即L1U1A1)标识活动。然后将这些sku存储在另一个表中。

我确定了以下内容以获得activity_id与product_id的列表:-

SELECT a.activity_id, w.product_id
FROM activities a
JOIN units USING(unit_id)
JOIN product_types USING(product_type_id)
JOIN web_products w
ON sku=CONCAT('L',SUBSTR(product_type_code,3), 'U',unit_index, 'A',activity_index)

我发现这是太复杂,以纳入一个SELECT在mysql,所以我创建了一个临时表,并加入了更新语句:-

CREATE TEMPORARY TABLE activity_product_ids AS (<the above select statement>);


UPDATE activities a
JOIN activity_product_ids b
ON a.activity_id=b.activity_id
SET a.product_id=b.product_id;

我希望有人觉得这有用

简单的MySQL:

UPDATE users AS U1, users AS U2
SET U1.name_one = U2.name_colX
WHERE U2.user_id = U1.user_id

您可以像这样使用内部连接从另一个表更新值

UPDATE [table1_name] AS t1 INNER JOIN [table2_name] AS t2 ON t1.column1_name] = t2.[column1_name] SET t1.[column2_name] = t2.column2_name];

跟随这里了解如何使用这个查询http://www.voidtricks.com/mysql-inner-join-update/

或者您可以使用select作为子查询来完成此操作

UPDATE [table_name] SET [column_name] = (SELECT [column_name] FROM [table_name] WHERE [column_name] = [value]) WHERE [column_name] = [value];

查询在这里详细解释http://www.voidtricks.com/mysql-update-from-select/

UPDATE [table_name] AS T1,
(SELECT [column_name]
FROM [table_name]
WHERE [column_name] = [value]) AS T2
SET T1.[column_name]=T2.[column_name] + 1
WHERE T1.[column_name] = [value];

你可以使用:

UPDATE Station AS st1, StationOld AS st2
SET st1.already_used = 1
WHERE st1.code = st2.code

对于同一张表,

UPDATE PHA_BILL_SEGMENT AS PHA,
(SELECT BILL_ID, COUNT(REGISTRATION_NUMBER) AS REG
FROM PHA_BILL_SEGMENT
GROUP BY REGISTRATION_NUMBER, BILL_DATE, BILL_AMOUNT
HAVING REG > 1) T
SET PHA.BILL_DATE = PHA.BILL_DATE + 2
WHERE PHA.BILL_ID = T.BILL_ID;

我有一个重复的条目在一个表本身的问题。以下是对我有效的方法。@sibaz也提倡这样做。

最后我用下面的查询解决了这个问题:

  1. 选择查询保存在临时表中

    IF OBJECT_ID(N'tempdb..#New_format_donor_temp', N'U') IS NOT NULL
    DROP TABLE #New_format_donor_temp;
    
    
    select *
    into #New_format_donor_temp
    from DONOR_EMPLOYMENTS
    where DONOR_ID IN (
    1, 2
    )
    
    
    -- Test New_format_donor_temp
    -- SELECT *
    -- FROM #New_format_donor_temp;
    
  2. The temp table is joined in the update query.

    UPDATE de
    SET STATUS_CD=de_new.STATUS_CD, STATUS_REASON_CD=de_new.STATUS_REASON_CD, TYPE_CD=de_new.TYPE_CD
    FROM DONOR_EMPLOYMENTS AS de
    INNER JOIN #New_format_donor_temp AS de_new ON de_new.EMP_NO = de.EMP_NO
    WHERE
    de.DONOR_ID IN (
    3, 4
    )
    

I not very experienced with SQL please advise any better approach you know.

Above queries are for MySql server.

如果您正在从一个复杂的查询进行更新。最好的方法是从查询创建临时表,然后使用临时表作为一个查询进行更新。

    DROP TABLE IF EXISTS cash_sales_sums;
CREATE TEMPORARY TABLE cash_sales_sums as
SELECT tbl_cash_sales_documents.batch_key, COUNT(DISTINCT tbl_cash_sales_documents.cash_sale_number) no_of_docs,
SUM(tbl_cash_sales_documents.paid_amount) paid_amount, SUM(A.amount - tbl_cash_sales_documents.bonus_amount - tbl_cash_sales_documents.discount_given) amount,
SUM(A.recs) no_of_entries FROM
tbl_cash_sales_documents
RIGHT JOIN(
SELECT
SUM(
tbl_cash_sales_transactions.amount
)amount,
tbl_cash_sales_transactions.cash_sale_document_id,
COUNT(transaction_id)recs
FROM
tbl_cash_sales_transactions
GROUP BY
tbl_cash_sales_transactions.cash_sale_document_id
)A ON A.cash_sale_document_id = tbl_cash_sales_documents.cash_sale_id
GROUP BY
tbl_cash_sales_documents.batch_key
ORDER BY batch_key;










UPDATE tbl_cash_sales_batches SET control_totals = (SELECT amount FROM cash_sales_sums WHERE cash_sales_sums.batch_key = tbl_cash_sales_batches.batch_key LIMIT 1),
expected_number_of_documents = (SELECT no_of_docs FROM cash_sales_sums WHERE cash_sales_sums.batch_key = tbl_cash_sales_batches.batch_key),
computer_number_of_documents = expected_number_of_documents,  computer_total_amount = control_totals
WHERE batch_key IN (SELECT batch_key FROM cash_sales_sums);
INSERT INTO all_table
SELECT Orders.OrderID,
Orders.CustomerID,
Orders.Amount,
Orders.ProductID,
Orders.Date,
Customer.CustomerName,
Customer.Address
FROM Orders
JOIN Customer ON Orders.CustomerID=Customer.CustomerID
WHERE Orders.OrderID not in (SELECT OrderID FROM all_table)