user11226067
user11226067

Reputation:

MYSQL Alias: Unable to give a name to new table

enter image description here

I have 3 tables given and I want to join all them and give this new table a name 'newtab'. I did this:

(select s1.Name as name, s1.ID as id, f1.Friend_ID as id, p1.Salary as sal
from Students as s1, Friends as f1, Packages as p1
where s1.ID=f1.ID AND s1.ID=p1.ID
) as newtab

now the problem is its showing error using 'as newtab'. How can I give this new table a name in MYSQL?

Upvotes: 0

Views: 63

Answers (2)

P.Salmon
P.Salmon

Reputation: 17665

The code segment on it's own is syntactically incorrect you need a main query

eg

SELECT * FROM
(select s1.Name as name, s1.ID as id, f1.Friend_ID as id, p1.Salary as sal
from Students as s1, Friends as f1, Packages as p1
where s1.ID=f1.ID AND s1.ID=p1.ID
) as newtab

you should also replace comma joins with explicit joins

'no but this is all you need to answer my question' - the answer to the question then is that as standalone code this is syntactically incorrect - but you know that already.

Upvotes: 1

Juned Khan
Juned Khan

Reputation: 122

You are trying to give a query an alias which is not possible, it's only plausible in [...from table_name as alias_name...]

select s1.Name as name, s1.ID as id, f1.Friend_ID as id, p1.Salary as sal

No need to give alias to the column name that have the same alias name.Alias are used when you want a different name Like:

select s1.Name as Student_Name, s1.ID as Student_ID, f1.Friend_ID, p1.Salary as sal

Please Note: if I don't specify alias then from the column s1.Name, "Name" will be displayed as column name by default.

select s1.Name as name, s1.ID as id, f1.Friend_ID as id, p1.Salary as sal
from Students as s1, Friends as f1, Packages as p1
where s1.ID=f1.ID AND s1.ID=p1.ID;

removing the "as newtab", the query should work fine.

Upvotes: 0

Related Questions