您好我正在尝试让mysql查询正常工作,但说实话我不知道从哪里开始,我认为计数功能会起作用但不确定。
我要做的是做一个select但是如果计数少于5行而不是与另一个query / where子句联合直到达到5。
所以基本上至少要排5行并继续加入行,直到达到5或更多...
$myquery_mysql = mysql_query("(SELECT title, city, ad_image, rent, uniqid FROM rentals WHERE front_page_ad = '1' AND paid = '1' AND city = '".mysql_real_escape_string($GLOBALS['user_city'])."')
UNION ALL
(SELECT title, city, ad_image, rent, uniqid FROM rentals WHERE front_page_ad = '1' AND paid = '1' AND province = '".mysql_real_escape_string($GLOBALS['user_province'])."' )
UNION ALL
(SELECT title, city, ad_image, rent, uniqid FROM rentals WHERE front_page_ad = '1' AND paid = '1' )" or die(mysql_error());
我会做点什么......
$myquery_mysql = mysql_query("(SELECT title, city, ad_image, rent, uniqid count(if title > 5) THEN FROM rentals WHERE front_page_ad = '1' AND paid = '1' )"
UNION ALL
(SELECT title, city, ad_image, rent, uniqid FROM rentals WHERE front_page_ad = '1' AND paid = '1' AND province = '".mysql_real_escape_string($GLOBALS['user_province'])."' )
ELSE FROM rentals WHERE front_page_ad = '1' AND paid = '1' )" END ;
or die(mysql_error());
我知道这不对,但我只想弄明白。
答案 0 :(得分:2)
您最后可以添加LIMIT 5
,因此它会对所有联盟执行整个查询,然后选择前5个结果,只要您没有给它一个ORDER BY它应该列出按照你们工会的顺序进行。
因此,如果第一个查询有5个结果,那么它只会采取这些结果,但如果没有,它将从接下来的2个联合中得到结果。只要您的工会不是非常耗时,这似乎是一个很好的行动方案。
答案 1 :(得分:0)
我会这样做:
desc
排序,因此匹配值的排序高于非匹配(将为null
)