In my schema, there are two tables with the same column names (Changing them is not an option)
Performing a query with
select * from tabA join tabB results in a mysql error of 'Duplicate column name col'
The only solution to this is to select using aliases, however I do not want to type alias.col for every column (since I need all columns from both tables)
Is it possible to do something along the lines of:
select tabA.(colA, colB, colC...), tabB.(colA, colB, colC...)
no its not possible.
you have to do like that
select tabA.colA, tabA.colB, tabA.colC..., tabB.colA, tabB.colB, tabB.colC...
if you have same name column in both tables , just give it an other alias like that.
lets say you have id column name in both tables.
select tabA.id , tabB.id as id_B
will give you result
id id_B
Linger's fiddle
Related
What I have tried:
CREATE TABLE temporary_bundle_wired
AS SELECT * FROM temporary_bundle,wired_items
WHERE temporary_bundle.id = wired_items.id;
I have two exisiting tables:
temporary_bundle
wired_items
I want to create a third table, called temporary_bundle_wired.
In this table I want to insert all rows (and their columns and fields) from temporary_bundle WHERE temporary_bundle.id = wired_items.id
I also want to delete those records from temporary_bundle once I have moved them into tempoary_bundle_wired.
The Query I have tried returns:
duplicate column name Id
The Query I have tried returns:
duplicate column name Id
The above error blatantly indicates that you have duplicate columns. The column names (id) in the new table you are creating are clashing between two old tables' common attributes (temporary_bundle.id and wired_items.id)
Make sure there are no other common attributes between two tables. If present then alias them from either of the table before inserting.
Try this if others are not working.
CREATE TABLE temporary_bundle_wired
AS
SELECT
t.id as id, user_id, base_item, extra_data, x, y, z, rot, wall_pos, limited_number, limited_stack, sandbox -- and all other attributes you want
FROM temporary_bundle t, wired_items w
WHERE t.id = w.id;
DELETING -
This is a different query all together.
DELETE from temporary_bundle t WHERE t.id = (select id from wired_items);
The problem is that both tables have a column named id, and your are selecting the two for creating the new table.
You will have to specify each column of one of the tables, so you can rename the id column:
CREATE TABLE temporary_bundle_wired
AS SELECT a.id as 'othername', user_id, base_item, ..., b.*
FROM temporary_bundle a, wired_items b
WHERE temporary_bundle.id = wired_items.id;
Cheers.
One problem is indeed that id is the same between the two tables. Other columns may be as well, but if id is the only problem then use the using clause:
CREATE TABLE temporary_bundle_wired AS
SELECT *
FROM temporary_bundle JOIN
wired_items
USING (id);
I need to join 3 tables which have some columns with same name, like id and some foreign keys columns.
I make a select query and the results come with table names only. How to get results like "dbname"."columnname" in my queries so I can identify from which table is each columns without having to specify every columns in the query (using only an *)?
Note: I use Delphi with ZeosLib, so a solution using these tools would be OK as well. But I prefer to set this in the data base.
You have to create an alias for your field name in your query
SELECT a.ID, b.ID
FROM a
JOIN b
You need doblue quote " for field names with special characters, so change it to.
SELECT a.ID "a.ID", b.ID "b.ID"
OR
SELECT a.ID "MeaningfullName", b.ID "OtherName"
For example here I have two fields name "sent_dt" and change one to previous_time
SQL Fiddle Demo
I have 2 tables connected via join. Both tables share some column names. I need to distinguish between those columns. So I am looking for something like this:
SELECT t1.* AS t1_* , t2.* AS t2_*
FROM t1
LEFT JOIN t2
ON t1.id=t2.somefield
Meaning if in both table is a column "place", the column names should appear in the result list as "t1_place" and "t2_place".
I did not find if this would be possible somehow. The reason is that both tables are rather large so I try to avoid listing all the columns and assigning aliases.
Any suggestions?
I have Table one and Table two. Table one has columns zoe and clo, but table two has oez and olc and it goes like this zoe=oez and cloe=olc how can I merge them?
I'm not sure if this is what you're looking for, but using union all can merge the results into as single table. You would have to, however, create a new primary key if this is going to be a permanent table.
Select a.id as id, a.value as value
From a
UNION ALL
Select b.id1 as id, b.value1 as value
From b
Group by id, value
Example:
http://www.sqlfiddle.com/#!2/093e1/4
I am wanting to select 1 column from my select statement as but then leave the rest as is so:
SELECT tbl_user.reference AS "reference", * FROM tbl_user JOIN tbl_details ON.....
Is this possible?
Yes. You can use double quotes like that to create a column alias. You can SELECT a column twice (or more) in your SELECT list.
Try something like this, where you can give each "reference" column its own alias:
SELECT u.reference AS UserReference,
d.reference as DetailsReference,
u.id, /*etc etc*/
FROM tbl_user AS U
JOIN tblDetails AS D ON ....
You mention in the comments that you want all columns from each table, while being able to distinguish between the reference columns(likely named the same in both tables). Suggest NOT using SELECT *, as it's an anti-pattern. It's most beneficial to specify your column list in your SELECT statement, and do your query engine a favour of not having to look up the list of columns on each table.
If you just want one column, this will work:
SELECT SELECT tbl_user.username AS "username" FROM tbl_user JOIN tbl_details on tbl_user.key LIKE tbl_details.key
What do you mean by "but then leave the rest as is "?