嘿大家,我正在试图找出一种方法来查询我的mysql服务器,这样即使公司当天没有发布任何内容并且用户点击了他们的徽标,它仍会将它们添加到列表中。 / p>
这听起来有些令人困惑,所以让我试着用另一种方式解释。
我的数据库中有3家公司:
Comp1
Comp2
Comp3
Comp1& Comp3在日历上有今天的东西,但Comp2没有。我仍然需要它来填充和放置该公司在页面上,但有一些东西沿着“今天的日历上没有任何东西”。其他两家公司(Comp1& Comp3)将显示当天的日历发布。
这是我现在的代码:
SELECT clientinfo.id, clientinfo.theCompName, clientinfo.theURL, clientinfo.picURL,
clientinfo.idNumber, clientoffers.idNumber, clientoffers.theDateStart, clientoffers.theDateEnd
FROM clientinfo, clientoffers
WHERE clientinfo.accountStats = 'OPEN'
AND clientinfo.idNumber = clientinfo.idNumber
AND '2010-05-08' BETWEEN clientoffers.theDateStart AND clientoffers.theDateEnd
GROUP BY clientinfo.idNumber
ORDER BY clientinfo.theCompName ASC
执行得很好但是对于Comp2,它只是将Comp1中的日历信息放入它中,当它真的没有任何东西时。
输出如下:
Comp1 | 2010-05-08 | this is the calendar event 1 | etc etc
Comp2 | 2010-05-08 | this is the calendar event 1 | etc etc <-- this should have no date or event just Comp2
comp3 | 2010-05-09 | this is the calendar event 2 | etc etc
输出需要的是:
Comp1 | 2010-05-08 | this is the calender event 1 | etc etc
Comp2 | | | etc etc
comp3 | 2010-05-09 | this is the calender event 2 | etc etc
我还要补充一点,如果公司的日历上没有事件,那么clientoffer表中没有idNumber。所以在上面的例子中,Comp2在clientoffer表中没有任何信息,所以我猜这就是为什么当我运行我的查询时它只复制了最后一条记录的数据,因为它在clientinfo.idnumber = clientoffer中找不到匹配项。 IDNumber中
任何帮助都会很棒:o)
大卫
更新#2
数据库结构:
clientinfo table:
theCompName | idNumber
comp 1 | 513200
comp 2 | 8944
comp 3 | 03884
clientoffers table:
idNumber | theDateStart | theDateEnd
513200 | 2010-05-08 | 2010-05-08
03884 | 2010-05-07 | 2010-05-09
希望有所帮助。
答案 0 :(得分:1)
我不确定我是否正确理解......但这有助于:
SELECT clientinfo.id
, clientinfo.theCompName
, clientinfo.theURL
, clientinfo.picURL
, clientinfo.idNumber
, IFNULL(clientoffers.idNumber,-1)
, IFNULL(clientoffers.theDateStart,'')
, IFNULL(clientoffers.theDateEnd,'')
FROM clientinfo
LEFT JOIN clientoffers
ON ( clientinfo.idNumber = clientoffers.idNumber
AND '2010-05-08' BETWEEN clientoffers.theDateStart
AND clientoffers.theDateEnd
)
WHERE clientinfo.accountStats = 'OPEN'
GROUP BY clientinfo.idNumber
ORDER BY clientinfo.theCompName ASC
您必须检查商品ID为-1,然后显示徽标。
这里的想法是从clientinfo中选择所有客户端,并显示来自客户端的连接信息,如果它不存在则显示为null。这就是左连接合成器的作用。
答案 1 :(得分:1)
这是我的理由。我有:
LEFT JOIN
查询WHERE
子句移至ON
子句GROUP BY
子句:如果公司有两个相同日期的事件,则返回两行所以我们有:
SELECT
clientinfo.idNumber,
clientinfo.theCompName,
clientoffers.theDateStart,
clientoffers.theDateEnd
FROM clientinfo
LEFT JOIN clientoffers ON
( clientinfo.idNumber = clientoffer.idNumber) AND
( '2010-05-08' BETWEEN clientoffers.theDateStart AND clientoffers.theDateEnd )
WHERE clientinfo.accountStats = 'OPEN'
ORDER BY clientinfo.theCompName ASC
我仍然需要知道这两个表正在使用哪些主键和外键。让我知道,我将修复查询。