2012-01-31 60 views
9

我是MYSQL的新手,無法解決,甚至有這麼多的答案在這個論壇上,無法確定在這個聲明中的錯誤。我正在使用MYSQL數據庫。更新與ORDER BY和LIMIT不工作在MYSQL

我有2個表:Ratemaster和費率,其中一個客戶可以有1個不同費率的產品。 因此,客戶和產品領域存在重複,只有利率領域發生了變化。 現在Table Ratemaster包含所有字段:ID,客戶代碼,產品,費率,用戶 而表格費率只有:ID,客戶代碼,費率,用戶。 - 用戶字段用於檢查session_user。

現在,表格Ratemaster有3條記錄,所有字段值相同,但速率字段爲空。 表格費率有不同的費率。 我希望所有費率都可以在費率表中更新。我無法與UPDATELIMIT mysql命令要做到這一點,它是給錯誤爲:

Incorrect usage of UPDATE and LIMIT

UPDATE Ratemaster, Rates 
SET Ratemaster.Rate=Rates.Rate 
WHERE Ratemaster.user=Rates.user 
LIMIT 1 
+1

你的'ORDER BY'在哪裏? (您的問題與ORDER BY說) – ManseUK 2012-01-31 13:38:59

+0

嗨,我已經嘗試與ORDER BY也,它給出了同樣的錯誤:UPDATE和ORDER BY的錯誤用法。 – user1114409 2012-01-31 13:43:11

+0

然後告訴我們,查詢 - 限制是沒有意義的ORDER BY – symcbean 2012-01-31 13:47:04

回答

15

通常你可以使用你的UPDATE聲明LIMITORDER,但在你的情況並不像寫在在MySQL Documentation 12.2.10. UPDATE Syntax

For the multiple-table syntax, UPDATE updates rows in each table named in table_references that satisfy the conditions. In this case, ORDER BY and LIMIT cannot be used.

嘗試以下操作:

UPDATE Ratemaster 
SET Ratemaster.Rate = 
(
    SELECT Rates.Rate 
    FROM Rates 
    WHERE Ratemaster.user = Rates.user 
    ORDER BY Rates.id 
    LIMIT 1 
) 
+0

在這種情況下,還應該做些什麼來使其發揮作用,請幫助解決我的問題。 – user1114409 2012-01-31 13:45:03

+0

添加了一個示例查詢,但我不確切知道你想要做什麼。 – 2012-01-31 13:59:22

+0

這將更新表'Ratemaster'的所有**行。 – 2012-01-31 14:06:47

-3

問題是LIMIT只能用於SELECT語句,因爲它限制了查詢返回的行數。

來源:http://dev.mysql.com/doc/refman/5.5/en/select.html

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants, with these exceptions:

Within prepared statements, LIMIT parameters can be specified using ? placeholder markers. 

Within stored programs, LIMIT parameters can be specified using integer-valued routine parameters or local variables as of MySQL 5.5.6. 

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):

SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15

To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:

SELECT * FROM tbl LIMIT 95,18446744073709551615;

With one argument, the value specifies the number of rows to return from the beginning of the result set:

SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rows

In other words, LIMIT row_count is equivalent to LIMIT 0, row_count.

For prepared statements, you can use placeholders. The following statements will return one row from the tbl table:

SET @a=1; PREPARE STMT FROM 'SELECT * FROM tbl LIMIT ?'; EXECUTE STMT USING @a;

The following statements will return the second to sixth row from the tbl table:

SET @skip=1; SET @numrows=5; PREPARE STMT FROM 'SELECT * FROM tbl LIMIT ?, ?'; EXECUTE STMT USING @skip, @numrows;

For compatibility with PostgreSQL, MySQL also supports the LIMIT row_count OFFSET offset syntax.

If LIMIT occurs within a subquery and also is applied in the outer query, the outermost LIMIT takes precedence. For example, the following statement produces two rows, not one:

(SELECT ... LIMIT 1) LIMIT 2;

+0

嗨,我需要的是從表B字段1更新與表A字段2其中所有記錄是重複的一個用戶,需要你的幫助來解決這種情況,與出限制,更新命令更新所有記錄相同的值 – user1114409 2012-01-31 13:49:50

+0

@ user1114409 :您指定的查詢將簡單地執行您告訴它的內容(即SET的內容),以查找與WHERE子句匹配的所有字段。你的桌子的佈局是什麼?也許Ratemaster.user = Rates.user比您想象的更頻繁地解析爲true。 – Karolos 2012-01-31 16:39:39

+0

嗨Karolos,是的,你是正確的where子句是我需要限制,以便它只更新記錄的匹配標準。在上面的問題中,我清楚地給出了表格的佈局,這很簡單。檢查第二段的相同。 – user1114409 2012-02-01 05:12:51

1

閱讀文章約 How to use ORDER BY and LIMIT on multi-table updates in MySQL

For the multiple-table syntax, UPDATE updates rows in each table named in table_references that satisfy the conditions. In this case, ORDER BY and LIMIT cannot be used.

+0

大聲笑,剛剛讀了那篇文章,我有一陣子震驚了。 這個人描述了一個項目,這個項目似乎完全是我工作的項目;) – John 2016-10-03 04:09:29

5

薩拉姆 您可以使用此方法,並正常工作!

UPDATE Ratemaster, Rates 
SET Ratemaster.Rate=Rates.Rate 
WHERE Ratemaster.user=Rates.user 
ORDER BY Rates.id 
LIMIT 1