1个表上带有名称ID的2个表 [英] 2 tables with an ID for name on 1 table

查看:57
本文介绍了1个表上带有名称ID的2个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用两个单独列的名称。



我的工作如下。但这只显示了宣传小费的人的名字/姓氏。



我希望能够连接名字和姓氏并使用dt.last_modified_user_id所在的位置。这是一个单独的选择声明吗?



我曾经使用过sql server并且可以通过查询编辑器来做到这一点 - 但我现在正在尝试使用MySQL,所以我对它进行了一些修改。



我尝试过:



选择dt.declare_time, p.first_name,p.last_name,dt.amount,dt.last_modified_time,dt.Last_Modified_User_ID from declared_tip dt

加入人员p在dt.person_id = p.id

I'm trying to use a name for 2 seperate columns.

What I had is working below. But this only shows the first name/last name for the person who has declared a tip.

I want to be able to concat the first name and last name and use that where dt.last_modified_user_id is. Would this be a seperate select statement?

I used to use sql server and would be able to do this via a query editor - but I'm now trying to use MySQL so I'm a little buffedled.

What I have tried:

select dt.declare_time, p.first_name, p.last_name, dt.amount, dt.last_modified_time, dt.Last_Modified_User_ID from declared_tip dt
join person p on dt.person_id = p.id

推荐答案

您需要加入person表的另一个实例。您可以使用JOIN子句中的表别名在SQL中执行此操作。



You'll need to join to another instance of the person table. You can do this in SQL by using a table alias in your JOIN clause.

SELECT dt.declare_time,
p.first_name,
p.last_name,
dt.amount,
dt.last_modified_time,
dt.Last_Modified_User_ID,
dtp.first_name,
dtp.last_name
FROM declared_tip dt
JOIN person p ON dt.person_id = p.id
JOIN person dtp ON dt.Last_Modified_User_ID = dtp.id





注意SELECT末尾的dtp字段。如果你想连接它们,你可以,但这会将它们显示为单独的字段。



Note the "dtp" fields at the end of the SELECT. If you want to concatenate them, you can, but this will show them as separate fields.


这篇关于1个表上带有名称ID的2个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆