结果小于限制时的mysql查询

时间:2012-09-12 22:27:01

标签: php mysql

这样可以正常工作,但是当它返回少于8个结果时会出现问题。 除了重复查询缺少的其余结果之外,还有其他选择吗? 任何帮助将不胜感激,谢谢。

       $vacation = mysql_query("SELECT i.* 
       FROM vacation_offer_tbl i
       INNER JOIN vacation_offer_pics_tbl c ON (c.vacat_offer_id = i.vacat_offer_id)
       WHERE i.conditional ='1' 
       AND c.pic_1 != ''
       // if < 8 results rerurn the rest where c.pic_1 ='nothing' 
       ORDER BY rand() 
       LIMIT 8") or die(mysql_error());

1 个答案:

答案 0 :(得分:1)

我不确定这对ORDER BY rand()的效果如何,但这样的事情可能会有所帮助

SELECT i.*, if(c.pic_1 != '',0,1) as sortOn
FROM vacation_offer_tbl i
INNER JOIN vacation_offer_pics_tbl c ON (c.vacat_offer_id = i.vacat_offer_id)
WHERE i.conditional ='1' 
ORDER BY sortOn, rand() 
LIMIT 8