JOIN 相同的 Table.Field 两次但用于不同的记录
JOIN the Same Table.Field Twice but for Different Records
我有这个 table,它有一个用户 id(int)
对 supervisor(int)
的请求,我需要获取每个用户的姓名和主管的姓名记录。到目前为止,这是我的代码:
SELECT
pdv_request.req_id,
pdv_request.data_caixa,
pdv_request.turno,
pdv_request.terminal,
pdv_request.nro_venda,
pdv_request.valor_venda,
pdv_request.razao,
usuarios.nome as username
FROM pdv_request
LEFT JOIN usuarios ON pdv_request.operador = usuarios.nome
LEFT JOIN usuarios ON pdv_request.supervisor = usuarios.nome
WHERE
pdv_request.data_caixa NOT BETWEEN :dini AND :dfin
ORDER BY
pdv_request.data_caixa ASC
usuarios
是 table 与用户(操作员、主管等)的详细信息,还有 :dini 和 :dfinal 只是 DAC 替换的参数日期,我需要为操作员和主管获取字段 nome
的内容。我很确定这是可能的,但我不知道如何在 select.
上引用第二个名字
对于select两个相同类型的表,以及来自这两个表的select字段,需要用别名来区分,像这样:
SELECT user1.nome as username1
SELECT user2.nome as username2
[...]
LEFT JOIN usuarios AS users1 ON pdv_request.operador = usuarios.nome
LEFT JOIN usuarios AS users2 ON pdv_request.supervisor = usuarios.nome
看看 MySQL 开发手册...
A select_expr can be given an alias using AS alias_name....
...
tbl_name [[AS] alias] [index_hint]
(Source: Dev.MySQL.com.)
我有这个 table,它有一个用户 id(int)
对 supervisor(int)
的请求,我需要获取每个用户的姓名和主管的姓名记录。到目前为止,这是我的代码:
SELECT
pdv_request.req_id,
pdv_request.data_caixa,
pdv_request.turno,
pdv_request.terminal,
pdv_request.nro_venda,
pdv_request.valor_venda,
pdv_request.razao,
usuarios.nome as username
FROM pdv_request
LEFT JOIN usuarios ON pdv_request.operador = usuarios.nome
LEFT JOIN usuarios ON pdv_request.supervisor = usuarios.nome
WHERE
pdv_request.data_caixa NOT BETWEEN :dini AND :dfin
ORDER BY
pdv_request.data_caixa ASC
usuarios
是 table 与用户(操作员、主管等)的详细信息,还有 :dini 和 :dfinal 只是 DAC 替换的参数日期,我需要为操作员和主管获取字段 nome
的内容。我很确定这是可能的,但我不知道如何在 select.
对于select两个相同类型的表,以及来自这两个表的select字段,需要用别名来区分,像这样:
SELECT user1.nome as username1
SELECT user2.nome as username2
[...]
LEFT JOIN usuarios AS users1 ON pdv_request.operador = usuarios.nome
LEFT JOIN usuarios AS users2 ON pdv_request.supervisor = usuarios.nome
看看 MySQL 开发手册...
A select_expr can be given an alias using AS alias_name....
...
tbl_name [[AS] alias] [index_hint]
(Source: Dev.MySQL.com.)