mySQL UNION 错误

发布于 2024-12-25 05:12:45 字数 646 浏览 0 评论 0原文

我在涉及联合的 sql 连接方面遇到问题。我试图从 2 个表中提取 COUNT 和一个字段,但出现错误。

查询:

$sql_result7 = mysql_query("(SELECT COUNT (*) as alertcount, date as alertdate FROM alerts WHERE to_id='$id' AND date > '$lastcheck') UNION (SELECT COUNT (*) as mailcount, date maildate FROM mobmail WHERE to_id='$id' AND to_del=0 AND seen = '0')", $db); 

$rs7 = mysql_fetch_array($sql_result7);
$alerts = $rs7[alertcount]; 
$mails = $rs7[mailcount]; 
$last_alert = $rs7[alertdate]; 
$last_mail = $rs7[maildate];

这与 date as alertdate 部分有关吗?

我得到的错误是:

警告:mysql_fetch_array():提供的参数不是有效的 MySQL 结果资源

Im having trouble with a sql join involving a union. I'm trying to pull a COUNT and a field from 2 tables but getting an error.

The query:

$sql_result7 = mysql_query("(SELECT COUNT (*) as alertcount, date as alertdate FROM alerts WHERE to_id='$id' AND date > '$lastcheck') UNION (SELECT COUNT (*) as mailcount, date maildate FROM mobmail WHERE to_id='$id' AND to_del=0 AND seen = '0')", $db); 

$rs7 = mysql_fetch_array($sql_result7);
$alerts = $rs7[alertcount]; 
$mails = $rs7[mailcount]; 
$last_alert = $rs7[alertdate]; 
$last_mail = $rs7[maildate];

Is it something to do with the date as alertdate part?

The error im getting is:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

不必你懂 2025-01-01 05:12:45

除了 COUNT(*) 之间的空格之外,还有另一个问题。您不能在 PHP 代码中使用 $rs7[mailcount]$rs7[maildate],因为您的查询相当于:

SELECT 
      COUNT(*) as alertcount
    , date as alertdate 
FROM alerts 
WHERE to_id = '$id' 
  AND date > '$lastcheck' 
UNION 
SELECT 
      COUNT(*)                      --- No "as mailcount" here
    , date                          --- No "as maildate" either 
FROM mobmail 
WHERE to_id = '$id' 
  AND to_del = 0 
  AND seen = '0'

并且将返回两行且仅 2 列:

alertcount | alertdate
-----------|------------
 24        | 2012-01-04
 73        | 2011-11-11

解决此问题的两种方法:

要么保留查询(将 UNION 更改为 UNION ALL 以确保始终获得 2 行),然后更改 PHP 以使用 2 行行。

或者将查询更改为:

SELECT alertcount, alertdate, mailcount, maildate 
FROM
      ( SELECT 
              COUNT(*) AS alertcount
            , date     AS alertdate 
        FROM alerts 
        WHERE to_id = '$id' 
          AND date > '$lastcheck' 
      ) AS a 
  CROSS JOIN
      ( SELECT 
              COUNT(*) AS mailcount
            , date     AS maildate 
        FROM mobmail 
        WHERE to_id = '$id' 
          AND to_del = 0 
          AND seen = '0'
      ) AS b

Besides the space between COUNT and (*), there is another issue. You can't use $rs7[mailcount] nor $rs7[maildate] in your PHP code because your query is equivalent to:

SELECT 
      COUNT(*) as alertcount
    , date as alertdate 
FROM alerts 
WHERE to_id = '$id' 
  AND date > '$lastcheck' 
UNION 
SELECT 
      COUNT(*)                      --- No "as mailcount" here
    , date                          --- No "as maildate" either 
FROM mobmail 
WHERE to_id = '$id' 
  AND to_del = 0 
  AND seen = '0'

and will return two rows and only 2 columns:

alertcount | alertdate
-----------|------------
 24        | 2012-01-04
 73        | 2011-11-11

Two ways to solve this problem:

Either keep the query (changing the UNION to UNION ALL to ensure that you always get 2 rows) and chnage the PHP to use the 2 rows.

Or change the query to:

SELECT alertcount, alertdate, mailcount, maildate 
FROM
      ( SELECT 
              COUNT(*) AS alertcount
            , date     AS alertdate 
        FROM alerts 
        WHERE to_id = '$id' 
          AND date > '$lastcheck' 
      ) AS a 
  CROSS JOIN
      ( SELECT 
              COUNT(*) AS mailcount
            , date     AS maildate 
        FROM mobmail 
        WHERE to_id = '$id' 
          AND to_del = 0 
          AND seen = '0'
      ) AS b
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文