Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
');
like image 771
Jimmyt1988 Avatar asked Jan 15 '23 08:01

Jimmyt1988


1 Answers

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
like image 97
juergen d Avatar answered Feb 05 '23 00:02

juergen d