一尘不染

sql,带内部联接的sqlite SELECT

sql

我想知道如何使用内部联接器以其他方式两次选择一列。我的数据库是sqlite,并且我使用PDO db驱动程序。

我的例子:

    SELECT
            orders.id,
            orders.order_number,
            clients.first_name,
            clients.last_name,
            users.name AS user_name
    FROM orders

    INNER JOIN clients ON
            orders.client_id = clients.id

    INNER JOIN users ON
            orders.created_by = users.id

我还想获得编辑此记录的用户名

            orders.edited_by = users.id

如何加入此选择?


阅读 155

收藏
2021-03-17

共1个答案

一尘不染

您将需要使用表别名。

SELECT
        orders.id,
        orders.order_number,
        clients.first_name,
        clients.last_name,
        creator.name AS creator_user_name
        editor.name AS editor_user_name
FROM orders

INNER JOIN clients ON
        orders.client_id = clients.id

INNER JOIN users creator ON
        orders.created_by = creator.id

INNER JOIN users editor ON
        orders.edited_by = editor.id
2021-03-17