2012-01-31 17 views
9

MYSQL konusunda yeni ve bu forumdaki bu sorunun cevabını tespit edemeyen çok fazla cevabı bile çözemiyorum. MYSQL veritabanı kullanıyorum.UPSATE, SIPARIŞ ve LIMIT ile çalışmaz MYSQL

2 tablom var: Bir müşterinin farklı oranlarda 1 ürüne sahip olduğu, fare tutucu ve fiyatlar. Bu nedenle, müşteri ve ürün alanlarının bir kopyası var, sadece oran alanı değişiyor. Şimdi Table Ratemaster'ın tüm alanları vardır: id, Müşteri kodu, Ürün, Oran, kullanıcı , Tablo Ücretleri ise sadece: id, cust code, Rate, user. - kullanıcı alanı, session_user öğesini kontrol etmek içindir.

Artık Table Ratemaster, Rate alanı hariç, tüm alan değerleriyle aynı olan 3 kayda sahiptir. Tablo Oranları farklı oranlara sahiptir. Tüm oranların Ratemaster'da Güncelleştirmeler tablosundan güncellenmesini istiyorum. Ben UPDATE ve LIMIT mysql komutu ile bunu kuramıyorum, o kadar hatayı veriyor ise: Genellikle size UPDATE tablolara LIMIT ve ORDER kullanabilirsiniz

Incorrect usage of UPDATE and LIMIT

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

Sizin 'SİPARİŞ BY'iniz nerede ??? (ORDER BY ile sorunuz) – ManseUK

+0

Merhaba, ORDER BY ile de denedim, aynı hatayı veriyor: UPDATE ve ORDER BY yanlış kullanımı. – user1114409

+0

Sonra bize bu sorguyu göstermek - SINIR – symcbean

cevap

15

ama sizin durumda değil, yazılı olarak MySQL Documentation 12.2.10. UPDATE Syntax:

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

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.

izlemeyi deneyin

+0

Böyle bir durumda, çalışmasını sağlamak için başka neler yapılmalı, lütfen sorunum için bir çözüm bulun. – user1114409

+0

örnek bir sorgu ekledi, ancak ne yapmak istediğinizi tam olarak bilmiyorum. –

+0

Bu, ** all ** satırındaki 'Ratemaster' satırını güncelleyecektir. –

-3

Sorun, LIMIT'in yalnızca sorgu tarafından döndürülen satır sayısını sınırladığı için SELECT ifadeleriyle kullanılması gerektiğidir. Gönderen

: 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

Merhaba, Tek ihtiyacım olan tek bir kullanıcı için tüm kayıtların çoğaltıldığı Tablo A alan2 ile Tablo B alan1'den güncelleme yapmak, bu durumu çözmek için yardıma gerek yok, UPDATE komutu tüm kayıtları aynı değerle günceller – user1114409

+0

@ user1114409 : Belirttiğiniz sorgu, WHERE yan tümcesiyle eşleşen TÜM alanlar için anlattığınız şeyi (yani, SET'in içeriğini) gerçekleştirecektir. Masanızın düzeni nedir? Belki de, Ratemaster.user = Rates.user düşündüğünüzden daha sık doğrular. – Karolos

+0

Merhaba Karolos, Evet, tam olarak neyi sınırlama ihtiyacım olan şeydir, böylece yalnızca eşleşen ölçütler için kayıt güncellenir. Yukarıdaki soruda tablonun düzenini açıkça verdim, çok basit. Aynı paragraf için 2. paragrafı kontrol edin. – user1114409

1

Oku makale hakkında 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, Sadece bu makaleyi okuyun ve bir an için şoktaydım. Adam, üzerinde çalıştığım tam olarak bir proje üzerinde çalıştığını açıkladı;) – John

5

Salam bu yöntemi kullanmak ve düzgün çalışması yapabilir!

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