#1222 - The used SELECT statements have a different number of columns

后端 未结 5 738
Happy的楠姐
Happy的楠姐 2021-02-13 19:55

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.

S         


        
5条回答
  •  醉话见心
    2021-02-13 20:36

    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))
    

提交回复
热议问题