0

Hi,

I am trying to get same field name from different tables.
And i am using like the below query to get the field name.
I am expecting some other queries to get distinct 'function_name' column from 'N' of tables.

SELECT function_name
FROM base_table
UNION
SELECT function_name
FROM firstcall_table
UNION
SELECT function_name
FROM secondcall_table

Thanks in advance,
Manimuthu

4
Contributors
4
Replies
19
Views
2 Years
Discussion Span
Last Post by diafol
0

Not sure what the problem is, but you want distinct, use UNION instead of UNION ALL, but you seem to be doing that already

Edited by diafol

0

Hello,

You did not really say what the issue was but if you want to get distinct values of function_name from the union you included you could use something like this:

    select distinct data1.function_name from 
    (SELECT function_name
    FROM base_table
    UNION
    SELECT function_name
    FROM firstcall_table
    UNION
    SELECT function_name
    FROM secondcall_table) as data1
    group by data1.function_name
0

If I understand your problem correctly, you simply need to alias your output column name...

SELECT function_name as base_function_name
FROM base_table
UNION
SELECT function_name as firstcall_function_name
FROM firstcall_table
UNION
SELECT function_name as secondcall_function_name
FROM secondcall_table

Edited by Taywin

0

Still unsure what you want. You have not explained further. I shall try to explain a bit more (AFAIK):

UNION on will ensure unique (distinct) entries across all tables (even within tables).

UNION ALL will return every (non-distinct) entry across all tables (and within tables).

The use of alias AS won't hurt, but where fieldnames of "common columns" do not match, the first fieldname in the UNION query is assumed (used). The issue comes when "common columns" have different datatypes

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.