MySQL连接2个表但重命名列,因为它们具有相同的名称

时间:2012-10-12 11:46:23

标签: php mysql join identity-column

我有两张桌子,

管理员,定价

  • admin包含列(id,date_created,type,value)
  • 定价包含列(id,date_created,relation,value)

我想做一个连接两个表的选择,其中pricing.relation = admin.id

如何重命名value,id和date_created行,使它们不会相互覆盖?

这是我正在尝试的事情:

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

1 个答案:

答案 0 :(得分:9)

您可以使用别名:

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