简体   繁体   English

从2个表中按ID选择SQL WHERE子句

[英]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. 我在MySQL中有2个表,在这两个表中,我都有商人ID,商人,分支和其他一些字段,一个表的名称为商人,另一个表为产品。

tbl_merchant : tbl_merchant:

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

tbl_product : 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 ? 我想知道如何显示SQL_WHERE子句通过product_id = 100101和商人_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. 该语句将把两个表连接在一起,其中主键形式商人等于产品中的商人ID。

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. 好的,所以如果我们看一下select之后的第一部分,应该清楚将要打印的两列是商人名和分支。 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). 从本质上讲,到目前为止,您已经拥有两个通过商户ID链接在一起的表,这很有意义,因为1个商户可以拥有许多产品(即一对多关系)。 I'm assuming that the merchant ID is unique. 我假设商家ID是唯一的。 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). 然后,联接将具有相同商人ID的所有行配对(这在表之一中是唯一的,因此保证是正确的)。 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. 更具体地说,您可以将其视为合格的叉积,其中tbl_product中的每个元组与tbl_merchant中的每个元组合并,然后根据条件tbl_product.merchant_id = tbl_merchant.merchant_id进行限定。

The last part of the query (WHERE clause) simply eliminates rows based on the conditions provided. 查询的最后一部分(WHERE子句)仅根据提供的条件消除行。

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 选择商人名称,从tbl_merchant内部联接中分支tbl_product ON(tbl_product.merchant_id = tbl_merchant.merchant_id)WHERE product_id = 100101 AND商人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 您可以使用JOIN解决此类查询

there are some good article to learn more about JOIN with visula explanation:: 有一些很好的文章,可以用visula解释来了解有关JOIN的更多信息:

1) http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/ "A Visual Explanation of SQL Joins" 1) http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/ “ SQL联接的可视解释”

2) http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins "Visual Representation of SQL Joins" 2) http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins “SQL联接的可视表示”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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