PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > Insert with selection question
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Insert with selection question

Réponse
 
LinkBack Outils de la discussion
Vieux 14/04/2008, 22h00   #1
Can I Get a Word In
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.
  Réponse avec citation
Vieux 14/04/2008, 23h36   #2
ThanksButNo
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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. :-)
  Réponse avec citation
Vieux 14/04/2008, 23h40   #3
ThanksButNo
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h02.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11216 seconds with 11 queries