Mysql 在不添加其他列的情况下将列值从一个表替换到另一个表中

Mysql 在不添加其他列的情况下将列值从一个表替换到另一个表中,mysql,Mysql,我有两个表服务和用户。下面是他们的模式 服务 id | service_type | created_by 1 | test | 1 2 | test | 1 3 | test | 1 4 | test | 2 id | username | password 1 | user1@example.com | ....... 2 | user2@example.com | ....... 用户 id | service_type | created_by

我有两个表
服务
用户
。下面是他们的模式

服务

id  | service_type | created_by

1   | test  | 1
2   | test  | 1
3   | test  | 1
4   | test  | 2
id | username | password

1   | user1@example.com | .......
2   | user2@example.com | .......
用户

id  | service_type | created_by

1   | test  | 1
2   | test  | 1
3   | test  | 1
4   | test  | 2
id | username | password

1   | user1@example.com | .......
2   | user2@example.com | .......
输出

id | service_type | created_by

1   | test  | user1@example.com
2   | test  | user1@example.com
3   | test  | user1@example.com
4   | test  | user2@example.com
服务
中,由
创建的
用户
表中的
id
。我希望在输出中显示
服务
表中的所有数据,包括所有列名,但将
中创建的所有ID替换为
用户
表中的
用户名

下面是我的查询,但问题是,它添加了另一个名为
username
的列。我不需要另一列,只需将user表中的列值替换为services表。有可能吗

select s.*, u.username from services o JOIN users u ON s.created_by = u.id;
原因


我目前正在将graphql连接到我的数据库,我需要添加
Query
以获取所有信息,但我需要用户名,而不是
创建的
中的ID,因为将ID提供给用户不是一个好主意。

使用SQL,如果要排除任何一列,必须显式选择所需的列。这意味着您不能使用通配符
s.*
动态选择每一列,而不选择由
值创建的

也没有办法“替换”SQL查询中的值。最好选择其他列/值,并使用所需的列名为其别名

以下是您必须如何编写此查询(请注意select子句中的“as created_by”):


*但问题是,它添加了另一个名为username*No的列。您的问题在于
限制1
-您添加它的目的是什么?另一个问题是使用星号-用确实需要的明确列名称替换它。@Akina limit 1只是用于测试。有超过6000条记录,因此无法提取所有记录。我会移除它。对此很抱歉。@Akina我只想要服务中的所有列,并将username from user中的列值替换为created_by of service。您正在购买N个苹果,希望其中一个苹果尝起来像橙色,但不想用橙色替换一个苹果。对不起,这只是给你解释一下。我很肯定你知道我在这里干什么。您需要一个别名并指定服务表的所有列。一般来说,如果不确定苹果,你就不能用橙色来代替它,对吗?同样的概念也适用于这里。我只需要服务中的所有列,并将username from user中的列值替换为服务的created_。这还不是全部。不要愚弄自己或任何人。