如何在多个条件下联接多个表

时间:2018-08-27 02:33:14

标签: mysql sql inner-join

我有六个桌子

online_transaction

| date | id | supplier_id | product code |

online_transaction_enc

| date | id | item |

offline_transaction

| date | id |

offline_transaction_enc

| date | id | item |

产品

| type | product_code |

供应商

| supplier_id | country |

  

选择日期(在“ 2018年6月1日”和“ 2018年7月30日”之间)且类型=“住户”且国家/地区=“马来西亚”

这些大概是我想要实现的。我想在日期和ID上在线和离线合并项目,因此我将所有项目组合在一起,然后再执行其他要求。 如何在MySQL中做到这一点?

2 个答案:

答案 0 :(得分:1)

尝试一下:

 select count(a.item) from 
    (select date, id, item from online_transaction_enc
    union 
    select date, id, item from offline_transaction_enc)a
    inner join 
    (
    select  date,id,supplier_id,productcode from online_transaction
    union 
    select  date,id,supplier_id,productcode from offline_transaction)b
    on a.date=b.date and a.id=b.id
    inner join supplier on b.supplier_id=supplier.supplier_id
    inner join products on b.productcode=products.product_code
    where a.date between '20180601' And '20180730' AND Type='household' AND country='Malaysia'

答案 1 :(得分:-1)

SELECT count(item)
FROM online_transaction_enc
INNER JOIN online_transaction_enc ON supplier_id
INNER JOIN products ON supplier_id
...
WHERE WHERE (date BETWEEN '2010-01-30 14:15:55' AND '2010-09-29 10:15:55')
;

此解决方案使用内部联接。我相信这就是您要寻找的。这是更多文档 http://www.mysqltutorial.org/mysql-inner-join.aspx