【问题标题】:mySQL Return value from 'users' table in 'messages' table where three columns would return a different usermySQL 从 'messages' 表中的 'users' 表返回值,其中三列将返回不同的用户
【发布时间】:2015-05-14 23:53:24
【问题描述】:

我一直在寻找并尝试了几次尝试来解决这个问题。我已经经历了 INNER JOIN、LEFT JOIN、SELECT 并且我只是没有取得进展。

我有一个包含 idfrom_user_idto_user_idtimeactioned_bydatemessage 列的消息表。在我的用户表中有idfirst_namelast_name 以及其他一些信息,例如电子邮件、密码、权限等。

消息表 from_user_idto_user_idactioned_by 中的列是对应于用户表中的 id 的 INT,我想将用户的名字和姓氏返回到新表。所以如果我的用户表看起来像:

id:1,名字:Dave,姓氏:某人
id: 2, first_name: John, last_name: 别人
id:3,first_name:管理员,last_name:用户

我的消息表看起来像:

id: 1, from_user_id:1, to_user_id:2, time:1425818720, actioned_by: 3, date:1425772800, message:这是一条好消息

然后我会回来:

id: 1, from_user_id: Dave Some, to_user_id:John Someelse, time:1425818720, actioned_by: Admin User, date:1425772800, message: 这是一条好消息

到目前为止,我最好的尝试是这个

SELECT `message_history`.`id`,`message_history`.`from_user_id`, `message_history`.`to_user_id`,`message_history`.`actioned_by` `message_history`.`time`, `message_history`.`date`, `message_history`.`message` 
       (SELECT `users`.`first_name`, `users`.`last_name` 
       FROM `users` 
       WHERE  `users`.`id`=`message_history`.`from_user_id`
       ) AS `from`,
       (SELECT `users`.`first_name`, `users`.`last_name` 
       FROM `users` 
       WHERE `users`.`id`=`message_history`.`to_user_id`
       ) AS `to`,
       (SELECT `users`.`first_name`, `users`.`last_name` 
       FROM `users` 
       WHERE `users`.`id`=`message_history`.`actioned_by`
       ) AS `actioned` 
FROM `message_history`

这在 mySQL 中是否可行,或者我是否也可以编写一个 PHP 函数来获取名称?

非常感谢

戴夫

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    非常感谢您的帮助,我不得不做一些小的调整,但最终通过这个查询到达那里

    select 
            message_history.id, message_history.from_user_id,  message_history.to_user_id, message_history.actioned_by, message_history.time, message_history.date, message_history.message,
            concat(from_user.first_name, ' ', from_user.last_name) as from1,
            concat(to_user.first_name, ' ', to_user.last_name) as to1,
            ifnull(concat(actioned_user.first_name, ' ', actioned_user.last_name),'') as actioned
        from 
            message_history 
            join users from_user on 
                message_history.from_user_id = from_user.id
            join users to_user on
                message_history.to_user_id = to_user.id
            left join users actioned_user on
                message_history.actioned_by = actioned_user.id

    【讨论】:

    • 我很抱歉,经典剪辑和过去在我的加入标准方面失败了,如果它提供帮助,请随时支持我的答案
    • 我已经接受了答案,不幸的是我需要 15 XP 才能对答案进行投票,一旦我得到这个数字,我就会回来给它一些金光闪闪;)
    【解决方案2】:

    尝试这样的事情(我想我得到了你所有的字段......)这假设唯一可能没有分配的 id 是 actioned_by...... 并且每个名称的 concat 会将它们返回为“first last”所以没有需要通过 php 组合它们

        select 
            `message_history`.`id`,`message_history`.`from_user_id`, `message_history`.`to_user_id`,`message_history`.`actioned_by` `message_history`.`time`, `message_history`.`date`, `message_history`.`message`,
            concat(from_user.first_name, '', from_user.last_name) as `from`,
            concat(to_user.first_name, '', to_user.last_name) as `to`,
            ifnull(concat(actioned_user.first_name, '', actioned_user.last_name),'') as `actioned`
        from 
            message_history 
            join users from_user on 
                message_history.from_user_id = users.id
            join users to_user on
                message_history.to_user_id = users.id
            left join users actioned_user on
                message_history.actioned_by = users.id  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-31
      • 1970-01-01
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      相关资源
      最近更新 更多