别名左联接上的列名

编程入门 行业动态 更新时间:2024-10-19 20:26:44
本文介绍了别名左联接上的列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我有两个表,并且两个表的主要标识符都使用名称"id".如果要对这两个表执行联接,该如何别名要与前一个表联接的表的id?

Let's say I have two tables, and both their primary identifiers use the name '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?

例如:

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

现在,site_id应该与sites_indexed.id链接.代表individual_data行的实际id与sites_indexed具有相同的 title .

Now, site_id is supposed to link up with sites_indexed.id. The actual id which represents the row for individual_data however has the same title as sites_indexed.

就个人而言,我喜欢为所有内容使用名称id,因为它可以使内容保持一致.但是,在对服务器端进行脚本编写时,这会使事情变得混乱.

Personally, I like to just use the name id for everything, as it keeps things consistent. When scripting server-side however, it can make things confusing.

例如

$var = $result['id'];

鉴于上述查询,这不会使解释器感到困惑吗?

Given the aforementioned query, wouldn't this confuse the interpreter?

无论如何,这是如何完成的?

Anyway, how is this accomplished?

推荐答案

而不是使用"SELECT *"选择所有字段,而应显式命名所需的每个字段,并根据需要使用AS对其进行别名.例如:

Instead of selecting all fields with "SELECT *" you should explicitly name each field you need, aliasing them with AS as required. 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.

更多推荐

别名左联接上的列名

本文发布于:2023-11-15 13:57:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1595053.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:别名

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!