mysql查询多次返回同一行

时间:2014-01-22 05:12:30

标签: mysql sql

我很难弄清楚为什么我的mysql查询返回同一行五次。这是下面的查询。我在这里读到关于连接和cartisian产品的一些东西。我对此非常陌生,并且在我的网站上编辑此查询是其他人最初创建的。试图理解这里使用的连接,但这让我很困惑。任何帮助是极大的赞赏。目标是让每一行只返回一次,而不是五次。谢谢!

SELECT off . * , resp.tracking_date, resp.paid_date, resp.total_value, resp.total_conversion
FROM tbl_offers AS off, tbl_tracking AS tr, tbl_trackvalue AS resp, tbloffers_cat AS offcat
WHERE off.id = tr.offer_id
AND off.offer_type = 'cash_offer'
AND off.id = offcat.offer_id
AND tr.id = resp.tracking_id
AND tr.user_id = '1454'
AND resp.total_value !=0
AND resp.payment_status = 'paid'
LIMIT 0 , 30
哇,谢谢大家的帮助。看起来这里提到的很多解决方案都可行。通过tracking_id向查询添加一个组,我确实得到了我想要的结果。不确定这是不是最好的方法。我只是在编辑旧网站时学习编程,我最初还是有其他人在构建。很有意思。整个连接是迄今为止我处理的更复杂的事情之一,但我相信它会变得更容易。

我将通过这些答案选择最好的答案,虽然看起来多个可以工作,所以我不确定这个网站上的标准采摘方法是什么。再次感谢!

4 个答案:

答案 0 :(得分:2)

虽然您的查询应该按预期工作,但是它使用了弃用的语法,我建议用标准显式连接替换已弃用的隐式连接:

SELECT off. * , resp.tracking_date, resp.paid_date, resp.total_value, resp.total_conversion
FROM tbl_offers AS off
JOIN tbl_tracking AS tr
 ON off.id = tr.offer_id
JOIN tbl_trackvalue AS resp
 ON tr.id = resp.tracking_id
JOIN tbloffers_cat AS offcat
 ON off.id = offcat.offer_id
WHERE off.offer_type = 'cash_offer'
  AND tr.user_id = '1454'
  AND resp.total_value !=0
  AND resp.payment_status = 'paid'
LIMIT 0 , 30

对于多条记录,如果JOIN条件未标识一对一关系,那么您将获得预期的多行。尝试从查询中的所有表中选择字段以查看哪些table有多个值被返回。也许有多个跟踪记录具有相同的offer_id,但您不会在查询中看到它,因为您没有从跟踪表中选择任何内容。

答案 1 :(得分:0)

您的查询中有cartesian product

您应该加入tbl_offers和所有其他类似的表

FROM tbl_offers a JOIN tbl_tracking u on a.id = u.offer_id

所以试试这个

SELECT off. * , resp.tracking_date, resp.paid_date, resp.total_value, resp.total_conversion
FROM tbl_offers AS off
JOIN tbl_tracking AS tr
 ON off.id = tr.offer_id
JOIN tbl_trackvalue AS resp
 ON tr.id = resp.tracking_id
JOIN tbloffers_cat AS offcat
 ON off.id = offcat.offer_id
WHERE off.offer_type = 'cash_offer'
  AND tr.user_id = '1454'
  AND resp.total_value !=0
  AND resp.payment_status = 'paid'
LIMIT 0 , 30

在此处详细了解如何加入表格:http://dev.mysql.com/doc/refman/5.5/en/join.html

答案 2 :(得分:0)

SQL JOINS

Codeproject有这个图片,它解释了SQL连接的简单基础知识,取自:

Visual Representation of SQL Joins

enter image description here

在此处更新您的查询

SELECT off.*,resp.tracking_date, resp.paid_date, resp.total_value, resp.total_conversion
FROM tbl_offers AS off
LEFT JOIN  tbl_tracking AS tr ON tr.offer_id = off.id
LEFT JOIN  tbl_trackvalue AS resp ON  resp.tracking_id = tr.id 
LEFT JOIN  tbloffers_cat AS offcat ON offcat.offer_id = off.id
WHERE  off.offer_type = 'cash_offer'
AND tr.user_id = '1454'
AND resp.total_value != 0
AND resp.payment_status = 'paid'
LIMIT 0 , 30

希望这能解决您的问题

答案 3 :(得分:0)

在查询中看起来不像tbloffers_cat,并且可能包含在结果集中不可见的行。要验证这一点,请在查询中添加tbloffers_cat.*,然后查看这些字段中的值是否因行而异。

如果是这种情况,请尝试:

SELECT off.*
    , resp.tracking_date
    , resp.paid_date
    , resp.total_value
    , resp.total_conversion
FROM tbl_offers AS off
    INNER JOIN tbl_tracking AS tr ON tr.offer_id = off.id
    INNER JOIN tbl_trackvalue AS resp ON resp.tracking_id = tr.id
WHERE off.offer_type = 'cash_offer'
AND tr.user_id = '1454'
AND resp.total_value !=0
AND resp.payment_status = 'paid'
LIMIT 0 , 30

在我看来,在查看查询时,内部联接的语法通常更清晰。