MySQL join 2 tables but rename columns because they have the same name

Jimmyt1988 picture Jimmyt1988 · Oct 12, 2012 · Viewed 8.1k times · Source

I have 2 tables,

admin, pricing

  • admin contains columns (id, date_created, type, value)
  • pricing contains columns (id, date_created, relation, value)

I want to do a select that joins the two tables where pricing.relation = admin.id

How do I rename the value, id and date_created rows so they do not overwrite each other?

This is the kinda thing i'm trying:

$sub_types = $database->query('
    SELECT 
    pricing.*,
    admin.*
        FROM 
        pricing,
        admin
            WHERE pricing.relation = admin.id
');

Answer

juergen d picture juergen d · Oct 12, 2012

You can use aliases:

SELECT p.id as pid, 
       p.date_created as pricing_date, 
       p.type, p.value as pricing_value,
       a.id as aid, 
       a.date_created as admin_date,
       a.relation, 
       a.value as admin_value
FROM pricing p
inner join admin a on p.relation = a.id