【问题标题】:Mysql returns null for rows that doesn't existMysql 为不存在的行返回 null
【发布时间】:2013-09-19 12:33:21
【问题描述】:

我有以下代码:

while ($row = mysql_fetch_array($result)){    
    $que ='select SUM(price) from prices_adverts where advert_id="7" and room_type_id="54" and (date >= "2013-09-20" AND date <"2013-09-21") order by price';    
    $que ='select SUM(price) from prices_adverts where advert_id="7" and room_type_id="55" and (date >= "2013-09-20" AND date <"2013-09-21") order by price';    and etc    

    $res=mysql_query($que) or die();    
    $rw=mysql_fetch_row($res);    
    $price= $rw['0'];    
}    

这将返回一些在数据库中具有价格的记录的总和,对于不存在的记录返回 $price 的 NULL/当房间没有特定日期的价格时,它在表中不存在/ 所以我的问题是如何获得仅存在的记录的结果???我不需要价格的 NULL 值,是否可以在外部访问 $price while ?如何?请帮忙,谢谢

我可以解释一下我到底需要什么,这可能会帮助你帮助我:) 上面我正在循环酒店房间以检查特定时期的房间费用。比我需要在循环外绘制按钮,这会将访问者转移到预订页面。但是,如果一家酒店在该日期没有可用的房价,我希望没有预订按钮。这就是为什么我需要弄清楚酒店是否至少有 1 间房间的价格。希望这会有所帮助

################################################# ######更新

第一个查询:我要取所有伦敦酒店的id-s

select id from adverts where town="London" limit 0, 5    

for($i=0;$i<$num_rows;$i++){    
$row=mysql_fetch_row($result);    
echo echo_multy_htl_results($row[0]);    
}    

这个函数 echo_multy_htl_results 是:

select a.article_title, a.town, a.small_image, a.plain_text, a.star_rating, a.numberrooms, rta.room_type_id, rt.bg_room_type,a.longitude, a.latitude, a.link_name, a.id from adverts a, rooms_to_adverts rta,room_types rt where a.id = rta.advert_id and rta.advert_id="3" and rta.room_type_id=rt.id and rt.occupants>="1" group by rt.bg_room_type order by rt.occupants ASC    

它获取 html 酒店广场和 room_types_id-s 的信息,并且它来自已添加的鳕鱼。你有什么建议?

【问题讨论】:

  • 那是什么编程语言?

标签: mysql isnull


【解决方案1】:

也许通过添加AND price IS NOT NULL

【讨论】:

  • php 我试过这个 AND price IS NOT NULL 但仍然为不存在的记录返回具有 NULL 值的行..
【解决方案2】:

手头的直接问题的解决方案可以是这个查询:

select  SUM(price) 
from prices_adverts 
where advert_id="7" 
    and room_type_id="54"  -- notice, we are filtering on room type here
    and (date >= "2013-09-20" AND date <"2013-09-21") 
group by room_type_id -- this makes no rows appear when there are no rows found in this case
order by price    

有对应行时返回1行,没有对应行时返回0行。

但是,您的问题似乎具有不同的性质。您的操作方案似乎是这样的:

  1. 从数据库中查询行 (room_type_ids)
  2. 将它们放在一个循环中
  3. 为每次迭代运行一个查询

这很糟糕。数据库非常擅长使用 JOIN 和其他适当的子句来解决这类问题。我建议使用这些功能,并在你的脑海中扭转局面。这样,您可以发出一个查询,返回您需要的所有数据。我相信这个可能是这样的查询,提供所有房间类型 ID 及其总价:

select room_type_id, SUM(price) 
from prices_adverts 
where advert_id="7"  -- notice: no filtering for room_type_id this time
    and (date >= "2013-09-20" AND date <"2013-09-21") 
group by room_type_id
order by price    

此查询列出所有有记录的 room_type_ids,而不列出那些没有记录的,并且在每个不同的 type_id 旁边,它都有总价格。您可以在此SQL fiddle 中查看结果。 (数据类型明显是off,这只是为了在操作中显示)

编辑 要让广告 ID 也类似于 room_type_ids:

select advert_id, room_type_id, SUM(price) 
from prices_adverts 
where  (date >= "2013-09-20" AND date <"2013-09-21") 
   -- notice: no filtering for room_type_id or advert id this time
group by advert_id, room_type_id
order by price    

这将包含三列:advert_id、room_type_id 和总价...

【讨论】:

  • Ppeterka 66,谢谢你帮助我。我测试了您的查询,当我回显 $num_rows 时,两个查询都得到 1 和 0。还有别的,这是因为我正在循环 advert_id-s,不仅是 room_type_id .. advert_id 也是从前一个查询传递的变量
  • 看起来是这样的: $que = 'select SUM(price) from prices_adverts where advert_id="'.$id.'" and room_type_id="'.$row['6'] .'" and (date >= "'.$checkinDate.'" AND date
  • @thecore7 我认为您应该粘贴更大的代码块,包括前面的查询,以便能够提供一个好的解决方案。同时,我更新了我的答案,在组中也有一个包含 advert_id 的版本。
  • Ppeterka 66,你的建议是改变选择 advert_id 的逻辑。在之前的查询中,我找到了所有位于伦敦的酒店的所有 adverts_id-s,以及可以容纳 3 位访客的房间的 room_type_id。这就是为什么在上面的查询中我有所有正确的酒店 id-s 和正确的房间 id- s 现在我正在检查每个房间的价格.. 但是有些房间没有价格.. 你明白为什么我不能更改逻辑/查询吗?
  • 没有不能改代码的事。特别是在这种情况下。我仍然建议您将代码再发布一个级别,以查看您的外部循环 zou 所做的事情,并使用 querz zou 来获取该数据。我相信有更好的方法来解决你想要的问题,退后一步,看看整个问题,而不是立即阻止你的问题......
【解决方案3】:

你可以使用

sum(case when price is null then 0 else price end) 

sum(isnull(price,0))

just add in your where clause `price is not null` to exclude them.

【讨论】:

  • price is not null 如果有价格的话会起作用,但是当这样的记录不存在时它返回 NULL
【解决方案4】:

你需要使用 HAVING

select SUM(price) 
from prices_adverts 
where advert_id="7" and room_type_id="54" and (date >= "2013-09-20" AND date <"2013-09-21")
having sum(price) is not null 
order by sum(price)

【讨论】:

  • 阿德里安你的建议接近我需要的。 $num_rows 为每个查询返回 1 0,但我怎么能说 while if($num_rows > 0){echo '';}
  • if (empty($results)) { echo '未找到结果'; }else {回显你的结果}
  • outside 虽然它只给我最后一个查询的 num_rows,所以如果你阅读我的编辑,这对我的目的无效
  • 我可以将所有查询中的 Num_rows 和 if > 0 相加来绘制按钮吗?当这个 num_row 的总和时,它会在外面可见吗?
  • 您可以有一个查询。只需使用“where room_type_id in (55,56,57,etc)”
猜你喜欢
  • 2017-12-14
  • 1970-01-01
  • 2021-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多