1

我需要选择现金支付且日期相同的交易,但仅适用于只有一次付款的交易(因此应从结果集中省略数字 14),因此正确的结果仅为 12 和 13。

    Table2                          Table1
num  | date | data | total       num | payment | date
12      xy     abc    2.5        12      cash     xy
13      xy     cbc    2.1        13      cash     xy
14      xy     acc    2.3        14      visa     xy
19      xy     def    2.0        14      cash     xy
27      xy     fgh    1.3        19      visa     xy
                                 27       mc      xy  

这样的事情在结果集中给出了 num 14 但应该省略 14。

SELECT num, data 
FROM Table2
WHERE num IN
(
SELECT num  FROM `Table1`
WHERE payment = 'cash'
GROUP BY `num`
HAVING ( COUNT(`num`) = 1  )
)        

总结正确答案(通过 tombom ):

 SELECT t2.num, t2.data 
 FROM Table1 as t1
 INNER JOIN Table2 as t2 ON t1.num = t2.num
 AND t1.date = 'xy'  
 GROUP BY t1.num
 HAVING GROUP_CONCAT(t1.payment) = 'cash'

谢谢!

4

1 回答 1

1

对不起,我完全误解了你的问题。以下是它的工作原理:

SELECT 
*
FROM
Table1 t1
INNER JOIN Table2 t2 ON t1.num = t2.num AND t1.date = t2.date 
GROUP BY t1.num
HAVING  GROUP_CONCAT(t1.payment) = 'cash'
于 2012-05-13T21:43:04.523 回答