2012-01-31 12 views
9

Jestem nowicjuszem w MYSQL, i nie jestem w stanie rozwiązać lub nawet z tak wieloma odpowiedziami na tym forum, nie jestem w stanie zidentyfikować błędu w tym komunikacie. Używam bazy danych MYSQL.AKTUALIZACJA z ORDER BY i LIMIT nie działa w MYSQL

Mam 2 tabele: Ratemaster i stawki, w których klient może mieć 1 produkt o różnych stawkach. Z tego powodu następuje dublowanie pól klienta i produktu, zmienia się tylko pole stawki. Teraz tabela Ratemaster ma wszystkie pola: identyfikator, kod klienta, produkt, stawka, użytkownik , podczas gdy ceny tabeli zawierają tylko: identyfikator, kod cust, cenę, użytkownik. - pole użytkownika służy do sprawdzania właściwości session_user.

Teraz tabela Ratemaster ma 3 rekordy, przy czym wszystkie wartości pola są takie same, z wyjątkiem pola Rate puste. Ceny Stołów mają różne stawki. Chcę, aby wszystkie stawki były aktualizowane w Rataaster z tabeli stawek. Jestem w stanie to zrobić z poleceniem UPDATE i LIMIT mysql, to daje błąd jako:

Incorrect usage of UPDATE and LIMIT

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

Gdzie jest twoje "ZAMÓWIENIE PRZEZ" ??? (twoje pytanie mówi Z ORDER BY) – ManseUK

+0

Witam, próbowałem również z ORDER BY, daje ten sam błąd: Niepoprawne użycie UPDATE i ORDER BY. – user1114409

+0

Następnie pokazują nam, że zapytanie - LIMIT jest bez znaczenia ORDER BY – symcbean

Odpowiedz

15

Zazwyczaj można użyć LIMIT i ORDER w swoich sprawozdaniach UPDATE, ale w Twoim przypadku nie, jak napisano w 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.

Spróbuj wykonać następujące czynności:

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

W takiej sytuacji, co jeszcze należy zrobić, aby to zadziałało, proszę pomóc w rozwiązaniu mojego problemu. – user1114409

+0

dodał przykładowe zapytanie, ale nie wiem dokładnie, co chcesz zrobić. –

+0

Spowoduje to aktualizację ** wszystkich ** wierszy tabeli 'Ratemaster'. –

-3

Problem polega na tym, że LIMIT można używać tylko z instrukcjami SELECT, ponieważ ogranicza liczbę wierszy zwracanych przez zapytanie.

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

Witam, potrzebuję tylko zaktualizować z pola Tabela 11 z Tabelą A pole2, gdzie wszystkie rekordy są duplikaty dla jednego użytkownika, potrzebuję twojej pomocy, aby rozwiązać tę sytuację, bez limitów, polecenie UPDATE aktualizuje wszystkie rekordy o tej samej wartości – user1114409

+0

@ user1114409 : Zapytanie, które podałeś, po prostu zrobi to, co mu kazałeś (tj. Zawartość SET) dla WSZYSTKICH pól zgodnych z klauzulą ​​WHERE. Jaki jest układ Twojego stołu? Może Ratemaster.user = Rates.user naprawia się częściej niż myślisz. – Karolos

+0

Cześć Karolos, Tak, masz rację, gdzie klauzula jest tym, co muszę ograniczyć, tak że aktualizuje się tylko dla rekordu dla pasujących kryteriów. W powyższym pytaniu wyraźnie podałem układ tabeli, jest bardzo prosty. Sprawdź drugi akapit dla tego samego. – user1114409

1

Przeczytaj artykuł o 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, Przeczytaj ten artykuł i przez chwilę byłem w szoku. Facet opisał pracę nad projektem, który wydaje się dokładnie tym, nad którym pracuję;) – John

5

Salam Można użyć tej metody i działa poprawnie!

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