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

Member Avatar

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

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

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
Member Avatar

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.