Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating a table using JOIN
    primarykey
    data
    text
    <p>I'm trying to compose an SQL statement but keep getting errors...</p> <p>Here is my situation: </p> <p>I have a table with sales orders and a table with item transfers. </p> <p>The sales orders have an order number and an extension, where the ID itself might not be unique, but the combination of order number and extension is. The extension can be null. </p> <p>The item transfer table has a reference number and sometimes a sales order number plus extension, but not always. </p> <p>The reason is that sometimes items are transferred for a sales order, sometimes for other reasons. Also, a sales order can happen without the item being transferred first. One order can only have one transfer though, and vice versa. </p> <p>I added a field "transref" to the sales order table so an order can be connected to a transfer, if applicable. (This info can't be computed on-the-fly for performance reasons.) So my tables now look like this (they are actually bigger but this is the important info):</p> <pre><code>SALESORDERS ORDERNO ORDEXT TRANSREF 1 (null) (null) 2 (null) (null) 2 a (null) 3 (null) (null) TRANSFERS TRANSREF ORDERNO ORDEXT t1 1 (null) t2 (null) (null) t3 2 a </code></pre> <p>Now I need to get the transref code into the sales orders table so that it looks like this: </p> <pre><code>SALESORDERS ORDERNO ORDEXT TRANSREF 1 (null) t1 2 (null) (null) 2 a t3 3 (null) (null) </code></pre> <p>I tried all kinds of statements, e.g.</p> <pre><code>UPDATE SALESORDERS INNER JOIN TRANSFERS ON SALESORDERS.ORDERNO = TRANSFERS.ORDERNO and (SALESORDERS.ORDEXT = TRANSFERS.ORDEXT or (SALESORDERS.ORDEXT is null and TRANSFERS.ORDEXT is null)) SET SALESORDERS.TRANSREF = TRANSFERS.TRANSREF WHERE TRANSFERS.ORDERNO IS NOT NULL </code></pre> <p>but nothing worked so far. Can someone help me untie the knot I got in my brain after trying a dozen tutorials and answers for this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload