使用distinct后获取重复项

时间:2013-06-26 13:48:56

标签: mysql sql

我有三个表,一个是映射故事,即使获取重复值

,我也使用了不同的关键字
SELECT
  mer.store_name,      mpr.`merchant_code` ,    mpr.`terminal_num` ,
  mpr.`rec_fmt` ,      mpr.`bat_nbr` ,          mpr.`Card_Type` ,
  mpr.`card_num` ,     mpr.`transaction_date` , mpr.`settle_date` ,
  mpr.`approval_code`, mpr.`intnl_amt` ,        mpr.`domestic_amt` ,
  mpr.`transid` ,      mpr.`upvalue` ,          mpr.`merchant_trackid` ,
  mpr.`MSF` ,          mpr.`service_tax` ,      mpr.`edu_cess` ,
  mpr.`net_amount` ,   mpr.`debit_credit_type`, mpr.`UDF1` ,
  mpr.`UDF2` ,         mpr.`UDF3` ,             mpr.`UDF4` ,
  mpr.`UDF5` ,         mpr.`seq_num` ,          mpr.`arn_no`
FROM
  `mpr_reports` mpr, merchantreports mer, storename_tid sid
WHERE (
  mer.Store_Name = sid.Store_Name
  AND sid.terminal_num = mpr.terminal_num
  AND mpr.`rec_fmt` = 'cvd'
  OR mpr.`rec_fmt` = 'bat'
)

2 个答案:

答案 0 :(得分:0)

关于SQL连接,您可能会有一些乐趣here ...
这是我们一生中学到的东西,在我们的一生中使用。

关于您的特定查询,请测试第一个示例

SELECT
  mer.store_name,
  mpr.`merchant_code`,
  mpr.`terminal_num`,
  mpr.`rec_fmt`,
  mpr.`bat_nbr`,
  mpr.`Card_Type`,
  mpr.`card_num`,
  mpr.`transaction_date`,
  mpr.`settle_date`,
  mpr.`approval_code`,
  mpr.`intnl_amt`,
  mpr.`domestic_amt`,
  mpr.`transid`,
  mpr.`upvalue`,
  mpr.`merchant_trackid`,
  mpr.`MSF`,
  mpr.`service_tax`,
  mpr.`edu_cess`,
  mpr.`net_amount`,
  mpr.`debit_credit_type`,
  mpr.`UDF1`,
  mpr.`UDF2`,
  mpr.`UDF3`,
  mpr.`UDF4`,
  mpr.`UDF5`,
  mpr.`seq_num`,
  mpr.`arn_no`
FROM merchantreports mer
INNER JOIN storename_tid sid ON (sid.Store_Name = mer.Store_Name)
INNER JOIN `mpr_reports` mpr ON (sid.terminal_num = mpr.terminal_num)
WHERE mpr.rec_fmt IN ('cvd','bat');

只是一个简单的建议,考虑使用外键加入你的表。索引列" Store_Name"和" terminal_num"也是一种选择,但要更加有效地遵循scriptures中的内容(见第5.4.5节)。

答案 1 :(得分:0)

考虑将已发布的查询修改为类似的内容......

SELECT mer.store_name
     , mpr.merchant_code 
     , mpr.terminal_num 
     , mpr.rec_fmt 
     , mpr.bat_nbr 
     , mpr.Card_Type 
     , mpr.card_num 
     , mpr.transaction_date 
     , mpr.settle_date 
     , mpr.approval_code 
     , mpr.intnl_amt 
     , mpr.domestic_amt 
     , mpr.transid 
     , mpr.upvalue 
     , mpr.merchant_trackid 
     , mpr.MSF 
     , mpr.service_tax 
     , mpr.edu_cess 
     , mpr.net_amount 
     , mpr.debit_credit_type 
     , mpr.UDF1 
     , mpr.UDF2 
     , mpr.UDF3 
     , mpr.UDF4 
     , mpr.UDF5 
     , mpr.seq_num 
     , mpr.arn_no
  FROM mpr_reports mpr
  JOIN storename_tid sid
    ON sid.terminal_num = mpr.terminal_num
  JOIN merchantreports mer
    ON mer.Store_Name = sid.Store_Name
 WHERE mpr.rec_fmt IN ('cvd','bat')
 ;