在 MySQL 中获取至少 x 行数

发布于 2024-11-28 04:22:02 字数 1261 浏览 1 评论 0原文

你好,我正在尝试让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

屌丝范 2024-12-05 04:22:03

您可以在末尾添加一个 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.

墨落成白 2024-12-05 04:22:03

我会这样做:

  • 左连接使用所有比较
  • 顺序按比较值desc,因此匹配值的顺序高于不匹配的值(将为null
  • 使用限制5

I'd do:

  • left join on using all the comparisons
  • order by the comparison values desc so matching values order higher than non-matching (which will be null)
  • use limit 5
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文