简体   繁体   中英

Selecting data from two mysql tables

I have two database tables, 1st is the TABLE_GLOBAL_PRODUCTS where all products information are stored.

store_id, product_id, product_name, product_description, product_price

The 2nd table is the TABLE_STORES where all stores information are stored.

store_id, store_name, store_country, store_tags, store_description

I have a search form where user can search for products where the store is on the selected country. Lets say, I want to search for cotton shirts where stores are located only on the US.

Now, In a mysql query, How can i select product_id, product_name, product_description, product_price in TABLE_GLOBAL_PRODUCTS where

store_id (in TABLE_GLOBAL_PRODUCTS) is the store_id (in TABLE_STORES) is the selected store_country . I hope I explained it clearer.

How to accomplish it or is there a way to accomplish it better?

Thanks.

This can be done by joining both tables Try this

SELECT p.*
FROM TABLE_GLOBAL_PRODUCTS p
INNER JOIN TABLE_STORES s ON s.store_id = p.store_id
WHERE s.store_country = 1 

Assuming 1 is the selected country

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM