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 767369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:01:06+00:00 2026-05-14T18:01:06+00:00

I have two tables: Stores and Items. The relationship is: Stores 1—* Items In

  • 0

I have two tables: Stores and Items. The relationship is: Stores 1—* Items

In PHP/MySQL what would be the best (fastest/simplest) way to check if a particular item belongs to a particular store.

In other words given for example:

$store_id = 1;
$item_id = 12;

I want to check if item 12 belongs to store 1 (and not some other store).

I usually do a select on Items matching both the store_id and item_id and limit the results to 1. Then check how many rows (0 or 1) were returned with mysql_num_rows. Is there a better way?

Update:

Both tables have an “id” column. The Items table has a “store_id” column.

  • 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-05-14T18:01:07+00:00Added an answer on May 14, 2026 at 6:01 pm
    SELECT COUNT(*) AS count
    FROM stores JOIN items USING(store_id)
    WHERE item_id = 12
      AND store_id = 1
    

    Then you’d get the results, and check of count > 0 or not. However, if I’m getting your DB design right, then you have a very messed up database.

    From what you describe, an item can only exist in one store. So my guess of the general layout here would be like this:

    STORE            ITEM
    -----            ----
    store_id ---|    item_id
    store_name  |--- store_id
    ...              item_name
                     ...
    

    Is this correct? An item can never exist except in the one store? So if it’s a screwdriver, every store would need a different item_id to hold it?

    A better design would be:

    STORE            STORE_ITEM         ITEM
    -----            ----------         ----
    store_id ------- store_id   |------ item_id
    store_name       item_id ---|       item_name
    ...                                 ...
    

    With a query of

    SELECT COUNT(*)
    FROM store JOIN store_item USING(store_id)
         JOIN item USING(item_id)
    WHERE store_id = 1
      AND item_id = 12
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables, one stores the products and quantity we have bought, the
We have two Tables: Document: id, title, document_type_id, showon_id DocumentType: id, name Relationship: DocumentType
I have two tables, one that stores the current price, and one that stores
I have two tables with a many-to-one relationship. (Oracle) **Table: PARENT** Field: A (PK)
I have 3 tables: Foods table stores all food items, Tags table stores all
I have two tables that are joined together. A has many B Normally you
I have two tables, Book and Tag , and books are tagged using the
I have two tables, both with start time and end time fields. I need
I have two tables containing Tasks and Notes, and want to retrieve a list
I have two tables in my database, called ratings and movies . Ratings: |

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.