联合中的MySQL查询错误

时间:2012-01-13 22:23:32

标签: php mysql union

  

可能重复:
  mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

我正在使用union进行查询,但似乎有一些错误,因为它无效。任何人都可以帮我解决这个问题吗?错误是:

  

警告:mysql_fetch_array()要求参数1为资源,第50行的\ class.php中给出布尔值

代码是:

$sql3=mysql_query(
        "SELECT * FROM ( ('select * from notification 
          where title_text='Global Notification' `user_id`=$frnnid 
          and `owner_user_id`=$frnnid and `is_seen`=0 
          order by `time_stamp` desc') 
        union 
        ('select * from notification 
         where title_text='Global Notification'  
         and user_id=$id and  owner_user_id=$frnnid and is_seen=0  
         order by time_stamp desc')
        ) notification order by Desc"
       );

5 个答案:

答案 0 :(得分:2)

如果这是您的查询:

SELECT * FROM ( ('select * from notification 
                  where title_text='Global Notification' 
                        `user_id`=$frnnid 
                    and `owner_user_id`=$frnnid 
                    and `is_seen`=0 
                  order by `time_stamp` desc') 
          union ('select * from notification 
                  where title_text='Global Notification'  
                    and user_id=$id and  
                    owner_user_id=$frnnid 
                    and is_seen=0  
                  order by time_stamp desc'
                 )
  ) notification order by Desc

然后一个问题是在AND之后和title_text之前缺少user_id。我把它隔开,你可以看到它在上面的位置。

我不知道这是否重要,但ORDER BY子句对内部select语句没有帮助。这两个部分在最后排序,因此内部排序不会做任何事情,甚至可能出现语法错误(我忘了)。

我的最后一点是,这些并不需要联合起来。

所有你需要的是:

'select * from notification 
 where title_text='Global Notification'  
   and user_id in ($id , $frnnid)
   and owner_user_id=$frnnid 
   and is_seen=0  
 order by time_stamp desc'

这似乎是唯一的区别(两个可能的user_id),为什么要联合呢?

答案 1 :(得分:2)

您的SQL语句格式错误,我在尝试使用您的SQL语句时遇到了很多错误。

尝试此查询:

SELECT * FROM 
(
  (
    SELECT * FROM notification 
    WHERE `title_text`='Global Notification' 
    AND `user_id`=$frnnid AND `owner_user_id`=$frnnid AND `is_seen`=0 
    ORDER BY `time_stamp` DESC
  ) UNION (
    SELECT * FROM notification 
    WHERE `title_text`='Global Notification' 
    AND `user_id`=$id AND `owner_user_id`=$frnnid AND `is_seen`=0 
    ORDER BY `time_stamp` DESC
  )
) AS notification ORDER BY time_stamp DESC;

答案 2 :(得分:1)

首先尝试在mysql中查看它是否有效然后你应该能够复制/粘贴你的代码..

    SELECT  * 
FROM (
select * from 
notification 
where 
title_text='Global Notification' 
and user_id = $frnnid 
and `owner_user_id`=$frnnid 
and `is_seen`=0  
union 
select * from 
notification 
where title_text='Global Notification'  
and user_id=$id 
and  owner_user_id=$frnnid 
and is_seen=0  
order by time_stamp desc) 

答案 3 :(得分:1)

您错过了一些AND以及最后一个ORDER BY后的字段名称,并且您有多余的单引号。如果这不能解决问题,您可能会在某些变量中包含错误的数据。使用mysql_error 来解决查询的确切问题。

$sql3 = mysql_query("
    SELECT *
    FROM (SELECT * 
          FROM notification 
          WHERE title_text = 'Global Notification' 
          AND user_id = $frnnid AND owner_user_id = $frnnid AND is_seen=0 
          ORDER BY `time_stamp` DESC
          UNION 
          SELECT * 
          FROM notification 
          WHERE title_text = 'Global Notification'  
          AND user_id = $id AND owner_user_id = $frnnid AND is_seen = 0  
          ORDER BY time_stamp DESC) notification
    ORDER BY `time_stamp` DESC
");

答案 4 :(得分:0)

仅根据查询的第一部分判断,您似乎在插入'时出现问题,为什么在select之前和desc之后出现问题? / p>

你需要摆脱那些。