Note that there are some explanatory texts on larger screens.

plurals
  1. POTwo records that have same data cell value in one field but different in another, query to return only one based on criteria
    primarykey
    data
    text
    <p>I need some help on my mysql syntax. </p> <p>My table has user information but has the same order number for both ship to and bill to. </p> <p>Sometimes people choose to ship to their billing address; I only need the order number record where users didn't enter a shipping addresses, and where they entered a shipping address.</p> <p>If they entered both bill to and shipping addresses, I only need the shipping address, if that makes sense. </p> <p>In the table there are sometimes duplicate fields for one order number. If that's confusing take a look below:</p> <pre><code>------------SQL TABLE ----------- order_id | address_type | user_name | address 100 | billto | Homer Simposn | 123 Main St. 100 | shipto | Homer Simpson | 455 XYZ Ave. 101 | billto | Peter Griffin | 780 111th St. 102 | billto | Charles Xavier | 555 Bergen St. 102 | shipto | Jean Gray | 555 Bergen St. </code></pre> <p>I need the output to be: </p> <pre><code>100 - shipto - Homer Simposn - 455 XYZ Ave. 101 - billto - Peter Griffin - 780 111th St. 102 - shipto - Jean Gray - 555 Bergen St. </code></pre> <p>Here is my syntax which will only return "shipto" records.</p> <pre><code> SELECT * FROM order_info AS A WHERE A.address_type = 'shipto' AND NOT EXISTS (SELECT B.address_type from order_info AS B where B.address_type = A.address_type and B.address_type = 'billto') </code></pre> <p>I would like to do something like <code>IF</code> there is no <code>shipto</code> then return the <code>billto</code>, Is this possible to do all in sql syntax?</p> <p>Thanks! </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.
    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