sql select多次返回同一组行

时间:2014-01-24 06:26:45

标签: mysql sql

我正在处理查询,并且它多次显示相同的行,只是在两个之间交替显示。我测试了从所有提到的表中选择每一列,并没有看到行之间的差异。请注意,应该只有两个返回的行,而且我已经超过了10.

我认为这与使用trv表加入mlc表有关,但我仍然坚持如何修复它。任何帮助表示赞赏。感谢。

SELECT mlc.click_amount AS memPayout
        FROM tbl_trackvalue AS trv, tbl_tracking AS t, tbl_offers AS off, tblusers AS usr, tbl_mailchimp_trackvalue as mlc
        WHERE mlc.tracking_id = trv.tracking_id
              AND off.id = t.offer_id
              AND off.offer_type = 'mailchimp'
              AND usr.id = trv.tr_user_id
              AND trv.tr_user_id =1454
              AND mlc.requested_date = '2014-01-21'
              AND mlc.payment_status = 'pending'
              AND mlc.requested_status = 'declined'

更新代码:

SELECT mlc.click_amount AS memPayout
                                        FROM tbl_trackvalue AS trv, tbl_offers AS off, tbl_mailchimp_trackvalue as mlc
                                        WHERE mlc.tracking_id = trv.tracking_id
                                        AND off.id = trv.tr_offer_id
                                        AND off.offer_type = 'mailchimp'
                                        AND trv.tr_user_id =1454
                                        AND mlc.requested_date = '2014-01-21'
                                        AND mlc.payment_status = 'pending'
                                        AND mlc.requested_status = 'declined'

更新II(再次更改,这次是有效的。感谢您的帮助。我非常新,并且现在意识到明确命名连接的重要性:

SELECT mlc.click_amount AS memPayout
    FROM  tbl_mailchimp_trackvalue as mlc inner join tbl_trackvalue AS trv on mlc.tracking_id=trv.tracking_id
    WHERE trv.type = 'mailchimp'
    AND trv.tr_user_id =1454
    AND mlc.requested_date = '2014-01-21'
    AND mlc.payment_status = 'pending'
    AND mlc.requested_status = 'declined'

2 个答案:

答案 0 :(得分:0)

消除可以使用的重复行SELECT DISTINCT

答案 1 :(得分:0)

您在t加入off并加入trvusr上的mlc,但这两个联接块之间绝不会有任何联接。