使用mysql中的join不能获得所需的结果

时间:2015-08-17 10:26:59

标签: mysql

我试过左,右,内连接这里是我的桌子

SHOP_OFFER_STATUS

    +-----------------------+--------------+------+-----+---------------------+----------------+
    | Field                 | Type         | Null | Key | Default             | Extra          |
    +-----------------------+--------------+------+-----+---------------------+----------------+
    | MOBILE_NUM            | varchar(100) | YES  |     | NULL                |                |
    | SHOP_ID               | varchar(100) | YES  |     | NULL                |                |
    | OFFER_ID              | int(255)     | YES  |     | NULL                |                |
    | OFFER_STATUS          | varchar(100) | YES  |     | NULL                |                |
    | OFFER_GENERATED_ID    | int(255)     | NO   | PRI | NULL                | auto_increment |
    | OFFER_GENERATED_DATE  | timestamp    | NO   |     | CURRENT_TIMESTAMP   |                |
    | OFFER_EXPIRY_DATE     | timestamp    | NO   |     | 0000-00-00 00:00:00 |                |
    | OFFER_CODE            | varchar(10)  | YES  |     | NULL                |                |
    | AMOUNT                | decimal(9,2) | YES  |     | NULL                |                |
    | BILL_AMOUNT           | decimal(9,2) | YES  |     | NULL                |                |
    +-----------------------+--------------+------+-----+---------------------+----------------+

TRACK_TRANSACTIONS

    +------------------+---------------+------+-----+-------------------+----------------+
    | Field            | Type          | Null | Key | Default           | Extra          |
    +------------------+---------------+------+-----+-------------------+----------------+
    | TRANSACTION_ID   | int(255)      | NO   | PRI | NULL              | auto_increment |
    | TRANCTION_TYPE   | varchar(100)  | YES  | MUL | NULL              |                |
    | TRANSACTION_DATE | timestamp     | NO   |     | CURRENT_TIMESTAMP |                |
    | AMOUNT           | decimal(10,2) | YES  |     | NULL              |                |
    | MOBILE_NUM       | varchar(15)   | YES  |     | NULL              |                |
    | SHOP_ID          | varchar(200)  | YES  |     | NULL              |                |
    +------------------+---------------+------+-----+-------------------+----------------+

我知道我的设计问题,我有成千上万的记录(无法回滚或重新设计),这里有人在交易表shop_id和mobile_num中兑换优惠时的工作原理添加我试图从这些表中获取BILL_AMOUNT,TRANSACTION_DATE ...但它给出错误的结果...我想知道何时(TRANSACTION_DATE)和他兑换的金额!

TRANSACTION表还有许多其他交易,例如usercreated,评论等(TRANCTION_TYPE),并且报价兑换是在TRANCTION_TYPE =' MERCHANT CASHBACK' 100条记录,如果我加入这个表超过50,000条记录进来:(这是查询!

 SELECT COUNT(*)
 FROM TRACK_TRANSACTIONS T RIGHT JOIN
      SHOP_OFFER_STATUS S
      ON T.SHOP_ID=S.SHOP_ID
 WHERE T.TRANCTION_TYPE='MERCHANT CASHBACK' AND S.OFFER_STATUS='REDEEMED';

所需的输出:100条记录,其中发生了报价兑换!

0 个答案:

没有答案