2012-01-31 14 views
9

Sono nuovo a MYSQL, e non riesco a risolvere o anche con così tante risposte su questo forum, incapace di identificare l'errore in questa affermazione. Sto usando il database MySQL.UPDATE con ORDER BY e LIMIT non funzionano in MYSQL

Ho 2 tabelle: Ratemaster e tariffe, in cui un cliente può avere 1 prodotto con tariffe diverse. Per questo motivo, c'è una duplicazione di campi cliente e prodotto, solo i cambi di campo vengono modificati. Ora Table Ratemaster ha tutti i campi: id, codice cliente, prodotto, tariffa, utente mentre Tabella tariffe ha solo: id, codice cliente, tariffa, utente. - il campo utente è per il controllo di session_user.

Ora Table Ratemaster ha 3 record con tutti i valori di campo uguali tranne il campo Rate vuoto. Le tariffe tabella ha tariffe diverse. Voglio che tutte le tariffe vengano aggiornate in Ratemaster dalla tabella delle tariffe. Non sono in grado di fare questo con il comando UPDATE e LIMIT mysql, è dando errore come:

Incorrect usage of UPDATE and LIMIT

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

Dov'è il tuo "ORDINA PER" ??? (la tua domanda dice con ORDER BY) – ManseUK

+0

Ciao, ho provato anche con ORDER BY, dà lo stesso errore: Uso errato di UPDATE e ORDER BY. – user1114409

+0

poi ci dimostrate che interrogazione - LIMIT è ORDER BY ha senso – symcbean

risposta

15

Di solito è possibile utilizzare LIMIT e ORDER nelle vostre UPDATE dichiarazioni, ma nel tuo caso non, come scritto in il 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.

provare quanto segue:

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

In tale situazione, che altro dovrebbe essere fatto per farlo funzionare, per favore aiutatemi con una soluzione al mio problema. – user1114409

+0

aggiunto una query di esempio, ma non so esattamente cosa si vuole fare. –

+0

Questo aggiornerà ** tutte le ** righe della tabella 'Ratemaster'. –

-3

Il problema è che LIMIT deve essere utilizzato solo con le istruzioni SELECT, in quanto limita il numero di righe restituite dalla query.

Da: 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

Ciao, tutto quello che mi serve è aggiornare dalla Tabella B campo1 con Tabella A campo2 dove tutti i record sono duplicati per un utente, hai bisogno del tuo aiuto per risolvere questa situazione, senza limite, comando UPDATE aggiorna tutti i record con lo stesso valore – user1114409

+0

@ user1114409 : La query specificata farà semplicemente ciò che gli hai detto (cioè il contenuto di SET) per TUTTI i campi che corrispondono alla clausola WHERE. Qual è il layout del tuo tavolo? Forse Ratemaster.user = Rates.user si risolve in true più spesso di quanto pensi. – Karolos

+0

Ciao Karolos, Sì, hai ragione, la clausola where è ciò che ho bisogno di limitare, in modo che aggiorni solo il record per i criteri di corrispondenza. Nella domanda di cui sopra ho chiaramente dato il layout del tavolo, è molto semplice. Controlla il 2 ° paragrafo per lo stesso. – user1114409

1

Leggi articolo su 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

Lol, leggi l'articolo e per un momento sono rimasto scioccato. Il ragazzo ha descritto il lavoro su un progetto che sembra essere esattamente quello su cui lavoro;) – John

5

Salam È possibile utilizzare questo metodo e funzionare correttamente!

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