Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8894543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:31:32+00:00 2026-06-14T23:31:32+00:00

I need some help on my mysql syntax. My table has user information but

  • 0

I need some help on my mysql syntax.

My table has user information but has the same order number for both ship to and bill to.

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.

If they entered both bill to and shipping addresses, I only need the shipping address, if that makes sense.

In the table there are sometimes duplicate fields for one order number. If that’s confusing take a look below:

------------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.

I need the output to be:

100 - shipto - Homer Simposn - 455 XYZ Ave. 
101 - billto - Peter Griffin - 780 111th St.
102 - shipto - Jean Gray - 555 Bergen St.

Here is my syntax which will only return “shipto” records.

    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')

I would like to do something like IF there is no shipto then return the billto, Is this possible to do all in sql syntax?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:31:33+00:00Added an answer on June 14, 2026 at 11:31 pm

    You could achieve this by finding the groupwise maximum for your row.

    Specifically, you use the order as the ID, and find the “maximum” address type in a subquery (it will find shipto if it’s there, and billto if not), and use that result to connect to rows in the outer query.

    For example (using an inefficient correlated subquery):

    SELECT * FROM order_info AS A WHERE A.address_type = (
       SELECT MAX(address_type) FROM order_info AS B 
       WHERE A.order_id = B.order_id )
    

    The above link gives an example of converting that to a more efficient join.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I need some code help. I've got an awkward little MySQL table that
I need some help optimizing a MySQL query or table When I run this
i need some help with this sql query heres my mysql table structure: DOMAINS
I need some help here. I have a mysql table called dictionary like this:
I need some help designing a friends system The mysql table: friends_ list -
I need some help in mysql statement Ive table1 with 7 column and table
I need some help with a group by mysql query clause. Medals Table (this
I need some help with a mySQL statement. I've got a mySQL table that
I need some help with a mysql query. I have 2 tables, Table 1
I need some help with some PHP and MySQL code. At the moment I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.