简体   繁体   English

在左连接上为列名称别名

[英]Alias a column name on a left join

Let's say I have two tables, and both their primary identifiers use the name 'id'. 假设我有两个表,并且它们的主要标识符都使用名称“id”。 If I want to perform a join with these two tables, how would I alias the id of the table that I want to join with the former table? 如果我想使用这两个表执行连接,我将如何为要与前一个表连接的表的id进行别名?

For example: 例如:

SELECT * FROM `sites_indexed` LEFT JOIN `individual_data` ON `sites_indexed`.`id` = `individual_data`.`site_id` WHERE `url` LIKE :url

Now, site_id is supposed to link up with sites_indexed.id . 现在,site_id应该与sites_indexed.id链接。 The actual id which represents the row for individual_data however has the same title as sites_indexed . 但是,表示individual_data行的实际idsites_indexed具有相同的标题

Personally, I like to just use the name id for everything, as it keeps things consistent. 就个人而言,我喜欢只使用名称id ,因为它保持一致。 When scripting server-side however, it can make things confusing. 然而,当编写服务器端脚本时,它会让事情变得混乱。

eg 例如

$var = $result['id'];

Given the aforementioned query, wouldn't this confuse the interpreter? 鉴于前面提到的查询,这不会混淆解释器吗?

Anyway, how is this accomplished? 无论如何,这是如何实现的?

Instead of selecting all fields with "SELECT *" you should explicitly name each field you need, aliasing them with AS as required. 您应该使用“SELECT *”选择所有字段,而应明确命名所需的每个字段,并根据需要使用AS对其进行别名。 For example: 例如:

SELECT si.field1 as si_field1,
       si.field2 as si_field2,
       ind_data.field1 as ind_data_field1
  FROM sites_indexed as si
  LEFT JOIN individual_data as ind_data 
         ON si.id = ind_data.site_id 
 WHERE `url` LIKE :url

And then you can reference the aliased names in your result set. 然后,您可以在结果集中引用别名。

This thread is old and i found because i had the same problem. 这个线程很老,我发现因为我有同样的问题。 Now i have a better solution. 现在我有了更好的解决方案。 The answer given by Paul McNett and antun forces you to list all fields but in some cases this is impossible (too much fields to list), so you can keep the * and alias only the fields you want (typically the fields that have the same name and will override each other). Paul McNett和antun给出的答案强制您列出所有字段,但在某些情况下这是不可能的(要列出的字段太多),因此您可以保留*和别名仅包含您想要的字段(通常是具有相同字段的字段)名称并将互相覆盖)。

Here's how : 这是如何做 :

    SELECT *, t.myfield as myNewName 
FROM table t ... continue your query

you can add as much aliases as you want by adding comas. 您可以通过添加逗号来添加任意多个别名。

The problem is that you're using the * wildcard. 问题是你正在使用*通配符。 If you explicitly list the column names in your query, you can give them aliases: 如果在查询中明确列出列名,则可以为它们指定别名:

SELECT `sites_indexed`.`id` AS `sites_indexed_id`,
       `individual_data`.`id` AS `individual_data_id`
       FROM `sites_indexed` 
       LEFT JOIN `individual_data` ON `sites_indexed`.`id` = `individual_data`.`site_id` 
       WHERE `url` LIKE :url

Then you can reference them via the alias: 然后你可以通过别名引用它们:

$var = $result['sites_indexed_id']; $ var = $ result ['sites_indexed_id']; $var_b = $result['individual_data_id']; $ var_b = $ result ['individual_data_id'];

Using this expression you will get results with columns id (from table sites_indexed ) and id2 (alias for column id from table individual_data ) 使用此表达式,您将获得具有列id (来自表sites_indexed )和id2 (来自表individual_data的id的别名)的结果

SELECT t1 . *, t2 . * FROM sites_indexed t1
LEFT JOIN (select id as id2, other_field1, other_field2 FROM individual_data) t2 ON t1.id = t2.site_id WHERE your_statement

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM