views:

58

answers:

3

hey guys

i managed to select from a table that saves my latest posts

but i need to have double condition in selection

here is my code :

$sql_query = "SELECT b.*,u.username AS MY_Sender
FROM TABLE_users u,TABLE_blogs b
Where b.reciever = '0' AND u.user_id = b.sender
UNION
SELECT b.*,u.username AS MY_reciever
FROM TABLE_users u,TABLE_blogs b
Where b.reciever != '0' AND u.user_id = b.reciever
ORDER BY bid DESC
LIMIT 0,7 ";

but MY_reciever is Null and empty

Am i wrong in using UNION for this need ?!

+2  A: 
SELECT b.*,u.username AS MY_Sender, NULL AS MY_reciever
FROM TABLE_users u,TABLE_blogs b
Where b.reciever = '0' AND u.user_id = b.sender
UNION
SELECT b.*, NULL AS MY_Sender, u.username AS MY_reciever
FROM TABLE_users u,TABLE_blogs b
Where b.reciever != '0' AND u.user_id = b.reciever
vartec
+1  A: 

Columns across the parts of a union must have the same name. Try:

SELECT b.*,
       u.username AS MY_User
  FROM TABLE_users u,
       TABLE_blogs b 
 WHERE b.reciever = '0' 
   AND u.user_id = b.sender 
UNION 
SELECT b.*,
       u.username AS MY_User
  FROM TABLE_users u,
       TABLE_blogs b 
 WHERE b.reciever != '0' 
  AND u.user_id = b.reciever 
 ORDER BY bid DESC 
 LIMIT 0,7

If you need to distinguish which part of the UNION a record has been returned by, return an extra column:

SELECT b.*,
       u.username AS MY_User.
       'Sender' AS MyType
  FROM TABLE_users u,
       TABLE_blogs b 
 WHERE b.reciever = '0' 
   AND u.user_id = b.sender 
UNION 
SELECT b.*,
       u.username AS MY_User.
       'Recipient' AS MyType
  FROM TABLE_users u,
       TABLE_blogs b 
 WHERE b.reciever != '0' 
  AND u.user_id = b.reciever 
 ORDER BY bid DESC 
 LIMIT 0,7
Mark Baker
its not working for finding the real poster , i mean exactly the condition : u.user_id = b.sender should be added to this statement as the real poster , how can i join that condition
Mac Taylor
I'm not sure I understand what you're asking
Mark Baker
In a UNION, columns must match ***data types***, not names.
OMG Ponies
I stand corrected
Mark Baker
+1  A: 

@Mac Taylor

hey man , sure you do understand me , told you that i need one more condition added to this statement you wrote , AS My_Real_Sender .. as you may know this statement you wrote works fine to find the Reciver ( MY_Type ) and not the sender of the post

You mean something like this?

SELECT b.*, 
       u.username AS MY_Sender. 
       NULL as MY_Recipient,
       'Sender' AS MyType 
  FROM TABLE_users u, 
       TABLE_blogs b  
 WHERE b.reciever = '0'  
   AND u.user_id = b.sender  
UNION  
SELECT b.*, 
       u1.username AS MY_Sender. 
       u2.username AS MY_Recipient. 
       'Recipient' AS MyType 
  FROM TABLE_users u1, 
       TABLE_users u2, 
       TABLE_blogs b  
 WHERE b.reciever != '0'  
  AND u1.user_id = b.reciever  
  AND u2.user_id = b.sender  
 ORDER BY bid DESC  
 LIMIT 0,7 
Mark Baker
thanks bro / i tested your script , but problem in reading and not worked at all , are you sure , there is no syntax error in above code ?!
Mac Taylor