0

たとえば、2 つのテーブルを作成しました。

表 1: t5zgu_property_message

msg_from msg_to subject message

57       42     xxxxx   xxxxxx
57       42     xxxxx   xxxxxx
57       42     xxxxx   xxxxxx
42       42     xxxxx   xxxxxx

表 2: t5zgu_users

id username

42 Jack
57 Rocky

このように出力したい:

msg_from msg_to subject message msg_from  msg_to

57       42     xxxxx   xxxxxx  Rocky     Jack
57       42     xxxxx   xxxxxx  Rocky     Jack
57       42     xxxxx   xxxxxx  Rocky     Jack
42       42     xxxxx   xxxxxx  Jack      Jack

私の現在のクエリは次のとおりです。

SELECT 
    t5zgu_property_message.id,
        t5zgu_property_message.msg_from,
        t5zgu_property_message.msg_to,
        t5zgu_property_message.subject,
        t5zgu_property_message.message,
        t5zgu_users.username as msg_from
FROM 
    t5zgu_property_message,
        t5zgu_users
WHERE
    t5zgu_property_message.msg_from = t5zgu_users.id

ORDER BY t5zgu_property_message.id DESC

このクエリはmsg_fromで完璧に機能し、正しい出力が得られますが、msg_toの書き方がわかりません。

アイデアや提案はありますか?ありがとう。

4

2 に答える 2

0

次のステートメントを試してください。

SELECT 
        p.id,
        p.msg_from,
        p.msg_to,
        p.subject,
        p.message,
        u1.username as msg_from
        u2.username as msg_to
FROM 
    t5zgu_property_message p LEFT JOIN 
        t5zgu_users u1 ON u1.id = p.msg_from
    LEFT JOIN t5zgu_users u2 ON u2.id = p.msg_to

ORDER BY p.id DESC
于 2013-08-22T09:14:37.693 に答える