简体   繁体   中英

Select from 2 table SQL WHERE Clause by ID

I have 2 tables in MySQL, in both of these tables I have merchant_id, merchant, branch and some another fields, the name of one table is merchant and another table is product.

tbl_merchant :

    id   |  merchant_id  |   merchant_name    |    branch      |   ...
   ------+---------------+--------------------+----------------+
    1    | 1001          |  McDonalds         |  branch 1 mcd  |   ...
    2    | 2002          |  KFC               |  branch 1 kfc  |   ...

tbl_product :

    id   |  product_id   |  product_name   |  price  | merchant_id
   ------+---------------+-----------------+---------+-------------
    1    | 100101        |  Chicken        | 10      | 1001
    2    | 100102        |  Potato         | 5       | 1001
    3    | 100101        |  Burger         | 10      | 2002
    4    | 100102        |  Fish Fillet    | 10      | 2002

I want to know how can to show merchant_name, branch from both tables using SQL WHERE Clause by product_id = 100101 and merchant_id = 1001 ?

Like this :

   Result :
    id   |  merchant   |  branch        |  product_name | price
   ------+-------------+----------------+---------------+-------
    1    | McDonalds   |  branch 1 mcd  |  Chicken      | 10

Thank You

Try this:

select * from marchant join product on marchant.id=product.merchant_id where merchant_id = 1001

This statement will join both tables together where the primary key form merchant is equals the merchant_id in product.

First, I'll show you the query, then I'll explain each part line by line to help you understand:

SELECT
   merchant_name, branch
FROM
   tbl_merchant INNER JOIN tbl_product ON (tbl_product.merchant_id = tbl_merchant.merchant_id)
WHERE
   product_id = 100101 AND merchant_id = 1001

Alright, so if we look at the first part following the select, it should be clear that the two columns that will be printed out are merchant_name and branch. Based on your output, you can print out any field from either table just by adding its name to the list. If the field has the same name in both tables, then you need to qualify it like this:

SELECT
  tbl_merchant.id, tbl_product.id
FROM
  tbl_merchant INNER JOIN tbl_product USING(merchant_id)

The tricky part of this query is the line that joins the two tables together. Essentially what you have as of now is two tables that are linked together by a merchant id, which makes sense because 1 merchant can have many products (ie a 1 to many relationship). I'm assuming that the merchant ID is unique. The join then pairs together all the rows that have the same merchant_id (which is unique in one of the tables and therefore guaranteed to be correct). More specially you can think of it as a qualified cross product where each tuple from tbl_product is joined with each tuple from tbl_merchant and then qualified based on the condition tbl_product.merchant_id = tbl_merchant.merchant_id.

The last part of the query (WHERE clause) simply eliminates rows based on the conditions provided.

The query for this is:

SELECT merchant_name, branch FROM tbl_merchant INNER JOIN tbl_product ON (tbl_product.merchant_id = tbl_merchant.merchant_id) WHERE product_id = 100101 AND merchant_id = 1001

SELECT merchants.id, merchants.merchant_id, merchants.branch, products.product_name, products.price 
FROM merchants 
INNER JOIN products ON products.merchant_id = merchants.merchant_id 
WHERE merchants.merchant_id = 1001 AND products.product_id = 100101

you can use JOIN to solve this type of query

there are some good article to learn more about JOIN with visula explanation::

1) http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/ "A Visual Explanation of SQL Joins"

2) http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins "Visual Representation of SQL Joins"

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