sql - problem with Update in MySQL -


According to the document, the update joins with the statement, works just as it is used in selection is. / P>

For example, if we have these two tables:

  mysql> Choose from the order; + --------- + ------------ + | Ordrid | Customer | + --------- + ------------ + | 1 | 1 | | 2 | 2 | | 3 | 3 | | 4 | 1 | + --------- + ------------ + mysql & gt; Choose from customers; + ------------ + ------------ + | Customer | Order Count | + ------------ + ------------ + | 1 | 9 | | 2 | 3 | | 3 | 8 | | 4 | 5 | | 5 | 7 | By using this select statement:  < Include customers with pre>  Select orders.customerid orders (customers.customerid = orders.customerid)  

Returns:

  + - --- ------- + | Customer | + ------------ + | 1 | | 1 | | 2 | | 3 | So, I was expecting the following statement:  
 Join customers.customerid = orders.customerid) to update order number for customer # 1 (customerid = 1) SET ordercount = ordercount + 1  

, but in fact it Not the case, here are the results after the update:

  mysql> Choose from customers; + ------------ + ------------ + | Customer | Order Count | + ------------ + ------------ + | 1 | 10 | | 2 | 4 | | 3 | 9 | | 4 | 5 | | 5 | 7 | + ------------ + ------------ +  

As you can see that only once The order is increased twice in the order table, and despite that, the selected statement correctly.

Is this a bug in MySQL or is it doing something wrong to me? I am trying to avoid using the group for performance reasons, so my interest is to understand what is happening.

In advance thanks

Yes, MySQL a record Updates each record as often as possible in the table.

I can not get this document, but practice says.

I will post it as a bug, so they will add it to the minimum document:

  Table updater (value not INT not NULL); Insert the uper value (1); Select from the uploader; Price --- 1 Update Updater (Select 1 New Union Union Select All 2) q SET u.value = u.value + newval; Select from the uploader; Value --- 2 (required 4)  

SQL server , by the way, behaves like one in more than one table UPDATE .

You can use:

update commands SET ordercount = ordercount + (SELECT COUNT (*) customers from C: customerid = o.customerid) < / Code>

is as long as you have an index on customer (customer_id)


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -