Mysql连接三个表(其中2个相似)

时间:2016-03-10 09:55:19

标签: mysql join

我的客户表

id | name
==========
1  | mick
2  | george
3  | john

面包店交易

id | customer | type    |  amount | date
============================================
1  | 3        | bread   |       1 | 2016/03/10 10:00
2  | 1        | bread   |       2 | 2016/03/10 11:00
3  | 1        | baguette|       1 | 2016/03/10 11:00
4  | 2        | bread   |       2 | 2016/03/11 09:00
5  | 3        | cookie  |       5 | 2016/03/11 09:30

蔬菜水果交易

id | customer | type    |  amount | date
============================================
1  | 1        | banana  |      1  | 2016/03/10 08:00
2  | 1        | apple   |      3  | 2016/03/10 08:00
3  | 1        | orange  |      5  | 2016/03/10 14:00
4  | 3        | apple   |      8  | 2016/03/11 09:30
5  | 1        | apple   |      8  | 2016/03/12 09:30

是否可以通过客户从这些交易表中按日期获得客户交易?

更具体地说,我想获得客户ID:1个交易,按日期排序;

这是我想要的。

Id| transactionType|customer | type    |  amount | date
=====================================================================
1 |  greengrocery  |       1 | banana  |      1  | 2016/03/10 08:00
2 |  greengrocery  |       1 | apple   |      3  | 2016/03/10 08:00 
2 |  bakery        |       1 | bread   |      2  | 2016/03/10 11:00
3 |  bakery        |       1 | baguette|      2  | 2016/03/10 11:00 
4 |  greengrocery  |       1 | orange  |      5  | 2016/03/10 14:00
5 |  greengrocery  |       1 | apple   |      8  | 2016/03/12 09:30

3 个答案:

答案 0 :(得分:1)

你基本上需要两个联合起来的联合查询,如下所示:

SELECT * FROM (
    SELECT t.id,'bakery' as TransType,s.id as customer,t.type,t.amount,t.date
    FROM customer s
    INNER JOIN bakery t ON(s.id = t.customer)
    UNION ALL
    SELECT t.id,'greengrocery' as TransType,s.id as customer,t.type,t.amount,t.date
    FROM customer s
    INNER JOIN greengrocery t  ON(s.id = t.customer)) tt
WHERE tt.customer = 1
order by tt.date

基本上,您甚至不需要从客户中进行选择,因为您没有使用名称值,因此可以这样做:

SELECT * FROM (
    SELECT t.id,'bakery' as TransType,t.customer as customer,t.type,t.amount,t.date
    FROM bakery t
    UNION ALL
    SELECT t.id,'greengrocery' as TransType,t.customer as customer,t.type,t.amount,t.date
    FROM  greengrocery t) tt
WHERE tt.customer = 1
order by tt.date

答案 1 :(得分:1)

最好有一个表用于具有类别字段的交易

id | category_id | customer | type    |  amount | date
============================================
1  |     1        |  3      | bread   |       1 | 2016/03/10 10:00

然后是类别表:

id | category_name | 
====================
1  |    bakery       

然后你可以像这样进行连接:

SELECT transactions.id, category_name AS transactionType, customer.name, transactions.type, tranasactions.amount, transactions.date
FROM transactions
LEFT JOIN categories ON transactions.category_id=categories.id
LEFT JOIN customers ON transactions.customer=customer.id

答案 2 :(得分:1)

使用UNION ALL生成行并使用变量生成新ID。

SET @id = 0;
( SELECT @id:=@id+1 id, 'bakery', customer, type, amount, date 
  FROM bakery_transactions 
  WHERE customer = 1 )
 UNION ALL
( SELECT @id:=@id+1 id, 'greengrocery', customer, type, amount, date 
  FROM greengrocery_transactions 
  WHERE customer = 1 )
ORDER BY date