This is a discussion on Insert with selection question within the MySQL forums, part of the Database Server Software category; --> Let me first describe the tables I'm working with: table1 with these columns: user_id cat_id more_data even_more_data table cat_id ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| Let me first describe the tables I'm working with: table1 with these columns: user_id cat_id more_data even_more_data table cat_id table1 may have one row for every cat_id in table2 which may be as many as 20 I want to make sure there is a row in table1 for every cat_id, such that every user_id has 20 rows in table. I'm trying to use an insert with a select to accomplish this. Is this even possible? Here is my query all comments appreciated. INSERT INTO table1 (cat_id) SELECT cat_id FROM table2 WHERE not exists (select cat_id from table1 where table1.user_id = 9999 ) where table1.user_id = 9999 The way I read this like this: insert into table1's cat_id column all cat_id's that exist in table2 but not in table2 for a given user_id. But this query fails with a not properly ended error. |
| |||
| On Apr 14, 2:00 pm, Can I Get a Word In <lore...@diespammerhurmans.com> wrote: "Can I Get a Word In" NO. Not until you learn how to make a proper subject line, one that briefly describes your problem. You problem is *not* "Can you get a word in," since clearly you just did! In fact, you got in 142 words by my count! :-D :-D :-D At any rate, without really looking in depth at your problem, your query is syntactically incorrect: INSERT INTO table1 (cat_id) SELECT cat_id FROM table2 WHERE not exists (select cat_id from table1 where table1.user_id = 9999 ) where table1.user_id = 9999 That last "where" should probably be an "and". Written a little differently for (perhaps) more clarity: INSERT INTO some_table (certain columns) SELECT (same columns) FROM some_table WHERE not exists (sub-query) WHERE some_table.some_column = some_value See? Two "where"'s won't work. You only get one to a customer. What probably you want is: INSERT INTO some_table (certain columns) SELECT (same columns) FROM some_table WHERE not exists (sub-query) AND some_table.some_column = some_value Now, of course your sub-query can have its own "where" clause. But again, it only gets ONE "where" clause. One to a customer. Sorry, I don't make the rules, I just inflict them. I hope you have better luck with it once you fix that. :-) |
| ||||
| On Apr 14, 3:36 pm, ThanksButNo <no.no.tha...@gmail.com> wrote: > On Apr 14, 2:00 pm, Can I Get a Word In > > <lore...@diespammerhurmans.com> wrote: > > "Can I Get a Word In" > > NO. Not until you learn how to make a proper subject line, one that > briefly describes your problem. > Ignore that. I got your subject confused with your screen name. Clearly the senility is taking full effect. They say there are three things that start to go when you get old. One is your memory, and I forget the other two. |