根据一个条件从两个表中过滤数据

时间:2017-09-25 16:53:52

标签: php mysql

我有2张桌子。

这是买表:

+--------+-----------------+---------------+
| id_buy | date            | market        |
+--------+-----------------+---------------+
|  6     | 2017-07-30      | Amazon        |
|  7     | 2017-08-02      | Amazon        |
| 17     | 2017-08-16      | Ebay          |
| 18     | 2017-08-09      | Banggood      |
| 19     | 2017-09-14      | Ebay          |
+--------+-----------------+---------------+

这是项目表:

+---------+----------------+-------------------------+
| id_item | id_buy         | items                   |
+---------+----------------+-------------------------+
|  6      |              6 | Filter Oli OH           |
|  7      |              6 | Filter Solar Halus OH   |
|  8      |              6 | Switch Klakson 12/24 V  |
|  9      |              6 | Kampas Rem 188 + Paku F |
| 10      |              7 | Kain Rem 188            |
| 11      |              7 | Kain Rem 200            |
| 23      |             17 | Kompresor Mesin         |
| 24      |             18 | Silikon                 |
| 25      |             19 | Kompresor Mesin Ganda   |
+---------+----------------+-------------------------+

我想根据市场字段过滤买入和物品表,因此它可以显示如下结果:

如果我使用market ='Amazon'过滤:

+--------+-----------------+---------------+-------------------------+
| id_buy | date            | market        | items                   |
+--------+-----------------+---------------+-------------------------+
|  6     | 2017-07-30      | Amazon        | Filter Oli OH           |
|  6     | 2017-07-30      | Amazon        | Filter Solar Halus OH   |
|  6     | 2017-07-30      | Amazon        | Switch Klakson 12/24 V  |
|  6     | 2017-07-30      | Amazon        | Kampas Rem 188 + Paku F |
|  7     | 2017-08-02      | Amazon        | Kain Rem 188            |
|  7     | 2017-08-02      | Amazon        | Kain Rem 200            |
+--------+-----------------+---------------+-------------------------+

如果我使用market ='Ebay'过滤:

+--------+-----------------+---------------+-------------------------+
| id_buy | date            | market        | items                   |
+--------+-----------------+---------------+-------------------------+
| 17     | 2017-08-16      | Ebay          | Kompresor Mesin         |
| 19     | 2017-09-14      | Ebay          | Kompresor Mesin Ganda   |
+--------+-----------------+---------------+-------------------------+

如果我过滤市场='Banggood':

+--------+-----------------+---------------+-------------------------+
| id_buy | date            | market        | items                   |
+--------+-----------------+---------------+-------------------------+
| 18     | 2017-08-09      | Banggood      | Silikon                 |
+--------+-----------------+---------------+-------------------------+

这是我显示结果的示例查询,但不是真的有效:

select buy.id_buy, buy.date, buy.market, item.items from buy, item where buy.id_buy in(17,19) and item.id_buy in(17,19);

这是查询结果:

+--------+-----------------+-----------+-----------------------+
| id_buy | date            | market    | items                 |
+--------+-----------------+-----------+-----------------------+
| 17     | 2017-08-16      | Ebay      | Kompresor Mesin       |
| 19     | 2017-09-14      | Ebay      | Kompresor Mesin       |
| 17     | 2017-08-16      | Ebay      | Kompresor Mesin Ganda |
| 19     | 2017-09-14      | Ebay      | Kompresor Mesin Ganda |
+--------+-----------------+-----------+-----------------------+

我很困惑为什么结果是双倍的。

1 个答案:

答案 0 :(得分:2)

问题在于您的加入。目前,您正在使用cross join buy, item,这将获得两个表的笛卡尔积。 (意思是,由于您还没有定义每个表中的哪些列代表关系,因此您会得到一个查询结果,该结果包含buy中每行的每一行,这解释了您的双重结果。)

您需要指定定义联接的列。

SELECT buy.id_buy, buy.date, buy.market, item.items
FROM buy INNER JOIN item on buy.id_buy = item.id_buy

然后你可以把你需要的东西放在where子句中。