Insert with selection question

This is a discussion on Insert with selection question within the MySQL Database forums, part of the Database Forums 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 ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-14-2008
Can I Get a Word In
 
Posts: n/a
Default Insert with selection question

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.
Reply With Quote
  #2 (permalink)  
Old 04-14-2008
ThanksButNo
 
Posts: n/a
Default Re: Insert with selection question

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. :-)
Reply With Quote
  #3 (permalink)  
Old 04-14-2008
ThanksButNo
 
Posts: n/a
Default Re: Insert with selection question

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.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:57 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0