SQL SELECT查询导致不同的列错误

时间:2012-05-20 11:05:52

标签: php mysql html sql

我正在尝试整理我的网站的邮件垃圾部分,这是证明麻烦,我之前的查询有问题,最后让它工作,但现在分页也造成麻烦,我收到此错误

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Arken\pagination\function.php on line 9
SELECT COUNT(*) as `num` FROM user_inbox WHERE user_inbox.receiver_user_id='4' AND user_inbox.mail_deleted ='1' UNION SELECT * FROM user_outbox WHERE user_outbox.sender_user_id='4' AND user_outbox.mail_deleted ='1'

The used SELECT statements have a different number of columns

第9行周围的代码如下所示

$query = "SELECT COUNT(*) as `num` FROM {$query}";
        $row = mysql_fetch_array(mysql_query($query)) or die($query."<br/><br/>".mysql_error());;
        $total = $row['num'];

第9行是mysql_fetch_array

我的user_inbox表格如下所示

+------------+---------------+------+-----+----------+
| Field             | Type          | Extra          |
+------------+---------------+------+-----+----------+
| message_id        | int(11)       | auto_increment |
| receiver_user_id  | int(11)                        |
| receiver_username | varchar(255)                   |
| sender_user_id    | int(11)                        |
| sender_username   | varchar(255)                   |
| mail_subject      | varchar(255)                   |
| mail_message      | text                           |
| mail_date_sent    | datetime                       |
| mail_viewed       | enum('0','1')                  |
| mail_deleted      | enum('0','1')                  |
+------------+---------------+------+-----+----------+

我的user_outbox表看起来像这样

+------------+---------------+------+-----+----------+
| Field             | Type          | Extra          |
+------------+---------------+------+-----+----------+
| message_id        | int(11)       | auto_increment |
| sender_user_id    | int(11)                        |
| sender_username   | varchar(255)                   |
| receiver_user_id  | int(11)                        |
| receiver_username | varchar(255)                   |
| mail_subject      | varchar(255)                   |
| mail_message      | text                           |
| mail_date_sent    | datetime                       |
| mail_viewed       | enum('0','1')                  |
| mail_deleted      | enum('0','1')                  |
+------------+---------------+------+-----+----------+

我真的很感激你们给予的任何帮助,它已经将近2天试图理清这部分网站,我需要完成它。 感谢您的任何反馈。

3 个答案:

答案 0 :(得分:3)

问题是你使用UNION返回不同数量的列。

SELECT COUNT(*) as `num` FROM user_inbox ...
UNION
SELECT * FROM user_outbox ...

你可以让MySQL为你计算总行数:

SELECT COUNT(*) AS `num`
FROM
(
     SELECT * FROM user_inbox ...
     UNION ALL
     SELECT * FROM user_outbox ...
) AS your_table_alias

或者:

SELECT 
    (SELECT COUNT(*) FROM user_inbox ...) +
    (SELECT COUNT(*) FROM user_outbox ...) AS `num`

答案 1 :(得分:3)

查询的第二部分(在UNION之后)也应该有COUNT(*)。

SELECT COUNT(*) as `num` FROM user_inbox WHERE user_inbox.receiver_user_id='4' AND user_inbox.mail_deleted ='1' UNION SELECT COUNT(*)  FROM user_outbox WHERE user_outbox.sender_user_id='4' AND user_outbox.mail_deleted ='1' 

答案 2 :(得分:0)

您应该对查询&#34; $ query&#34;进行更多研究。看起来您在查询中为变量定义了不同数量的列&#34; $ query&#34;在你的PHP(我推测)代码中(至少错误说明了这一点)。