在 MySQL 中获取至少 x 行数
你好,我正在尝试让 mysql 查询正常工作,但说实话,我不知道从哪里开始,我认为 count 函数可以工作,但不确定。
我想做的是进行选择,但如果计数的行数少于 5 行,则与另一个查询/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());
我知道这是不对的,但我只是想弄清楚。
Hi I am trying to get a mysql query to work properly but to be honest Im not sure where to start, I think count function would work but not sure.
What I am trying to do is do a select but if there are less than 5 rows counted than union with another query/where clause until 5 is reached.
So basically go at least 5 rows and keep joining rows until 5 or more is reached...
$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());
Would I do something like...
$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());
I know this isn't right, but I am just trying to figure it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在末尾添加一个
LIMIT 5
,这样它将使用所有联合执行整个查询,然后选择前 5 个结果,只要您不给它 ORDER BY 就应该按照您进行工会的顺序列出它们。因此,如果第一个查询有 5 个结果,它将只获取这些结果,但如果没有,它将获取接下来 2 个并集的结果。只要您的工会不太耗时,这似乎就是一个不错的行动方案。
You could add a
LIMIT 5
at the end, so it will do the entire query with all the unions and then select the top 5 results, as long as you're not giving it an ORDER BY it should list them in the order you do your unions.So if the first query has 5 results it'll just take those, but if not, it'll take results from the next 2 unions. As long as your unions aren't very time consuming this seems like a good course of action.
我会这样做:
desc
,因此匹配值的顺序高于不匹配的值(将为null
)I'd do:
desc
so matching values order higher than non-matching (which will benull
)