-1

I have done allot of reading about this and it seems that isn't possible. The thing is, since the amount of columns is dynamic i cant explicitly mention them. I put in aliases but the 'id' column keeps on appearing.

The CREATE doesnt work since there is 3 id columns in there, obiously.

(This query actually is ok, it works so dont evaluate it please)

CREATE TABLE merged as 
    SELECT *, 
           wuwebusers.id as wuwebusers_id, 
           jgwebusers_address.id as jgwebusers_address_id, 
           jgwebusers.id as jgwebusers_id 
    FROM wuwebusers 
    LEFT OUTER JOIN jgwebusers_address ON wuwebusers.id = jgwebusers_address.userid 
    LEFT OUTER JOIN jgwebusers ON wuwebusers.id = jgwebusers.userid 
    GROUP BY wuwebusers.id

EDIT: question: how can i select every column except a few explicit ones. I know SELECT * isn't the correct way, since i am selecting all. I am asking for other way(s).

Since i have to do this a few times and the columns are dynamic and quite a few it isn't maintainable by explicitly writing every column name.

aynber
  • 22,380
  • 8
  • 50
  • 63
San Jay Falcon
  • 993
  • 1
  • 9
  • 20

1 Answers1

1

First you are doing a SELECT * Then adding columns as well. * is everything therefore you are getting mulitples of the same column. Those 3 ID's that you gave distinct names are being SELECTed again by the * in your query. Remove the * and type in the columns you need and you'll be fine.

CREATE TABLE merged as 
SELECT 
    wuwebusers.id as wuwebusers_id, 
    jgwebusers_address.id as jgwebusers_address_id, 
    jgwebusers.id as jgwebusers_id , 
    AllotherColumnsYouNeed as WhateverYouWantToCallthem
FROM wuwebusers 
LEFT OUTER JOIN jgwebusers_address 
    ON wuwebusers.id = jgwebusers_address.userid 
LEFT OUTER JOIN jgwebusers 
    ON wuwebusers.id = jgwebusers.userid 
    GROUP BY wuwebusers.id

/Edit\ If you changed your tables to not use the generic method of just labling a column ID in the first place you would also solve this problem. If you have a table name product for example call the ID column for that table ProductID. This will also solve your problem.

Zane
  • 4,129
  • 1
  • 21
  • 35