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

  • SEARCH
  • Home
  • 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 8300835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:42:10+00:00 2026-06-08T16:42:10+00:00

i have the following mysql tables with some sample data. 1) table: ai_shipment +—-+———————+———————+——————+

  • 0

i have the following mysql tables with some sample data.

1) table: ai_shipment

+----+---------------------+---------------------+------------------+
| id | booking_date        | loading_date        | container_number |
+----+---------------------+---------------------+------------------+
|  1 | 2012-08-03 00:00:00 | 2012-08-04 00:00:00 | ABB987987BBC6    |
|  2 | 2012-08-05 00:00:00 | 2012-08-07 00:00:00 | BHJKKU78786GH    |
+----+---------------------+---------------------+------------------+

2) table: ai_purchase_item

+----+---------+----------+-----------+-----------+
| id | item_id | quantity | cost      | rate      |
+----+---------+----------+-----------+-----------+
|  1 |       1 |       50 | 1200.0000 | 1355.0000 |
|  2 |       2 |       20 |  550.0000 |  675.0000 |
|  3 |       4 |       70 |   70.0000 |   70.0000 |
|  4 |       6 |       90 |   90.0000 |   90.0000 |
|  5 |       7 |       80 |   80.0000 |   80.0000 |
+----+---------+----------+-----------+-----------+

3) table: shipment_purchase_item

+----+-------------+------------------+
| id | shipment_id | purchase_item_id |
+----+-------------+------------------+
|  1 |           1 |                2 |
|  2 |           2 |                3 |
+----+-------------+------------------+

basically i am storing all the purchased items details in ai_purchase_item, ai_shipment stores the shipping details, and ai_shipment_purchase_item stores records of items that is shipped.

what i am trying to do is, i want to select all records from ai_purchase_item that is not shipped. which means the foreign key should not exist in ai_shipment_purchase_item.

with reference to above records the result i am expecting to be fetched is.

+----+---------+----------+-----------+-----------+
| id | item_id | quantity | cost      | rate      |
+----+---------+----------+-----------+-----------+
|  1 |       1 |       50 | 1200.0000 | 1355.0000 |
|  4 |       6 |       90 |   90.0000 |   90.0000 |
|  5 |       7 |       80 |   80.0000 |   80.0000 |
+----+---------+----------+-----------+-----------+

i tried something like this (i know the sql query is not proper)

 SELECT 
    pi.id, 
    pi.item_id, 
    pi.quantity, 
    pi.cost,
    pi.rate 
 FROM 
    ai_purchase_item pi 
 JOIN 
    ai_shipment_purchase_item spi ON spi.purchase_item_id = pi.id
 WHERE NOT EXISTS (SELECT spi.purchase_item_id WHERE spi.purchase_item_id = pi.id)

thank you.

  • 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-08T16:42:11+00:00Added an answer on June 8, 2026 at 4:42 pm

    There are three common approaches for finding values present in one table but missing in another in MySQL:

    • LEFT JOIN
    • NOT EXISTS
    • NOT IN

    Here’s the approach using NOT EXISTS:

    SELECT 
        pi.id, 
        pi.item_id, 
        pi.quantity, 
        pi.cost,
        pi.rate 
    FROM 
        ai_purchase_item AS pi 
    WHERE NOT EXISTS
    (
        SELECT *
        FROM ai_shipment_purchase_item AS spi
        WHERE spi.purchase_item_id = pi.id
    )
    

    And here’s a LEFT JOIN:

     SELECT 
        pi.id, 
        pi.item_id, 
        pi.quantity, 
        pi.cost,
        pi.rate 
     FROM 
        ai_purchase_item AS pi 
     LEFT JOIN 
        ai_shipment_purchase_item AS spi ON spi.purchase_item_id = pi.id
     WHERE spi.purchase_item_id IS NULL
    

    And here’s NOT IN:

    SELECT 
        pi.id, 
        pi.item_id, 
        pi.quantity, 
        pi.cost,
        pi.rate 
    FROM 
        ai_purchase_item AS pi 
    WHERE pi.id NOT IN
    (
         SELECT purchase_item_id
         FROM ai_shipment_purchase_item
    )
    

    The following article by Quassnoi compares the performance of each of these approaches:

    • NOT IN vs. NOT EXISTS vs. LEFT JOIN / IS NULL: MySQL

    The conclusion is:

    the best way to search for missing values in MySQL is using a LEFT JOIN / IS NULL or NOT IN rather than NOT EXISTS.

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

Sidebar

Related Questions

I have a MySQL table with following data (sample); UserId - DeviceId - StartDate
I have three mysql table from same database Db1. Three tables have following columns.
I have the following two tables in my mysql database. Table Name: groups id
I have the following destination , image and destination_image tables: (MYSQL): destination: id_destination image:
I have 2 mysql tables : Question with the following columns : id, question,
I have 2 mysql tables 1. questions: with the following columns: id, title, answer1,
I have the following Mysql Table Order table status 0 - incomplete status 1
I have the following MySQL table structure: num field company phone website 1 Gas
I have a following mysql table with a lot (3000+) of values: [id] [parent_id]
I have the following MySQL/InnoDB table. I added a compound index as the primary

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.