Insert subquery error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hoonew

    #1

    Insert subquery error

    I'm baffled. Given the table defs below, the following query returns an
    error (ERROR 1136 (21S01): Column count doesn't match value count at row 1):

    mysql> insert into b (blue, green) values((SELECT red, alpha from a
    WHERE red=1));

    Obviously I'm doing something wrong, but what? How to make it work?
    For now I did a:

    INSERT INTO b SET
    blue=(SELECT red FROM a WHERE red=1),
    green=(SELECT alpha FROM a WHERE red=1);

    Not very efficient or pretty...

    CREATE TABLE `a` (
    `red` int(11) default NULL,
    `alpha` int(11) default NULL
    );

    CREATE TABLE `b` (
    `blue` int(11) default NULL,
    `green` int(11) default NULL
    );
  • Felix Geerinckx

    #2
    Re: Insert subquery error

    On 01/09/2005, hoonew wrote:
    [color=blue]
    > insert into b (blue, green) values((SELECT red, alpha from a WHERE[/color]
    red=1));

    insert into b (blue, green) SELECT red, alpha from a WHERE red=1;

    --
    felix

    Comment

    • hoonew

      #3
      Re: Insert subquery error

      Felix Geerinckx wrote:[color=blue]
      > On 01/09/2005, hoonew wrote:
      >
      >[color=green]
      >>insert into b (blue, green) values((SELECT red, alpha from a WHERE[/color]
      >
      > red=1));
      >
      > insert into b (blue, green) SELECT red, alpha from a WHERE red=1;
      >[/color]

      Boy, I feel dumb. Simplicity was never my strong suit.

      Comment

      Working...