views:

85

answers:

3

Why am i getting a #1222 - The used SELECT statements have a different number of columns ? i am trying to load wall posts from this users friends and his self.

SELECT u.id AS pid, b2.id AS id, b2.message AS message, b2.date AS date FROM 
(
    (
        SELECT b.id AS id, b.pid AS pid, b.message AS message, b.date AS date FROM 
        wall_posts AS b 
        JOIN Friends AS f ON f.id = b.pid 
        WHERE f.buddy_id = '1' AND f.status = 'b'
        ORDER BY date DESC
        LIMIT 0, 10
    )
    UNION
    (
        SELECT * FROM
        wall_posts
        WHERE pid = '1'
        ORDER BY date DESC
        LIMIT 0, 10
    )
    ORDER BY date DESC
    LIMIT 0, 10
) AS b2 
JOIN Users AS u
ON b2.pid = u.id
WHERE u.banned='0' AND u.email_activated='1'
ORDER BY date DESC
LIMIT 0, 10

The wall_posts table structure looks like id date privacy pid uid message

The Friends table structure looks like Fid id buddy_id invite_up_date status

pid stands for profile id. I am not really sure whats going on.

+1  A: 

You're taking the UNION of a 4-column relation (id, pid, message, and date) with a 6-column relation (* = the 6 columns of wall_posts). SQL doesn't let you do that.

dan04
Any work arounds?
Keverw
Change the latter `SELECT` to select the 4 columns you need. You shouldn't be using `SELECT *` anyway: It's vulnerable to breaking whenever someone does an `ALTER TABLE`.
dan04
+4  A: 

The first statement in the UNION returns four columns:

SELECT b.id AS id, 
       b.pid AS pid, 
       b.message AS message, 
       b.date AS date 
  FROM wall_posts AS b 

The second one returns six, because the * expands to include all the columns from WALL_POSTS:

SELECT b.id, 
       b.date, 
       b.privacy,
       b.pid. 
       b.uid message
  FROM wall_posts AS b 

The UNION and UNION ALL operators require that:

  1. The same number of columns exist in all the statements that make up the UNION'd query
  2. The data types have to match at each position/column

Use:

FROM ((SELECT b.id AS id, 
             b.pid AS pid, 
             b.message AS message, 
             b.date AS date 
        FROM wall_posts AS b 
        JOIN Friends AS f ON f.id = b.pid 
       WHERE f.buddy_id = '1' AND f.status = 'b'
    ORDER BY date DESC
       LIMIT 0, 10)
      UNION
      (SELECT id,
              pid,
              message,
              date
         FROM wall_posts
        WHERE pid = '1'
     ORDER BY date DESC
        LIMIT 0, 10))
OMG Ponies
Thank You! That helped fix my problem. Helpful to know for later down the road also.
Keverw
+1  A: 
(
        SELECT b.id AS id, b.pid AS pid, b.message AS message, b.date AS date FROM 
        wall_posts AS b 
        JOIN Friends AS f ON f.id = b.pid 
        WHERE f.buddy_id = '1' AND f.status = 'b'
        ORDER BY date DESC
        LIMIT 0, 10
    )
    UNION
    (
        SELECT id, pid  , message , date  
        FROM
        wall_posts
        WHERE pid = '1'
        ORDER BY date DESC
        LIMIT 0, 10
    )

You were selecting 4 in the first query and 6 in the second, so match them up.

Sabeen Malik