using INSERT and SELECT DISTINCT

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

using INSERT and SELECT DISTINCT

Post by duk »

hy guys

if we want to INSERT into a table 3 values, and 1 of them at this query need to be always 1, and the SELECT DISTINCT we gona use just get 2 values, and the third one need to be 1 how can i do this ???

this is the query:

Code: Select all

mysql> insert into cidpid (pid,cid,lider) select distinct members.pid,clans.cid
from members,clans where clans.pid_criador='3nrq74z';
but, the value for lider, dont come from the SELECT DISTINCT need to be 1 in this query but i cant figure how to insert it :(
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

I really don't understand what your doing, but if your SELECT, is only returning 2 results and you need 3 for the INSERT, then just put the value you need in the SELECT part of your query!

A simple example..... ( SELECT 2 values add the third value => 1 )

Code: Select all

INSERT INTO tablea (pid, cid, lider) SELECT fielda, fieldb, 1 FROM tableb WHERE some_column = some_value;

Notice I inserted the value (1) after fieldb => , 1, I am only selecting fielda, fieldb and using (1) as the third value for the insert!


pif
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post by duk »

nice

thansk a lot m8..
Post Reply