1

我在下面有这个 sql 查询,注意receiver从 id 返回的字段table_1

我的问题是如何在现场获得接收FirstName器?LastNamereceiver

SELECT DISTINCT
    FirstName, 
    LastName, 
    table_2.status, 
    (select FirstName, LastName from table_1 where table_1.id = table_2.receiver) as receiver 
FROM table_1
INNER JOIN 
    table_2 on table_1.key_2 = CONCAT('OFFLINE-', table_2.id)
ORDER BY FirstName, LastName

我在我的 SQL 中添加了一个子查询,但这给了我一个语法错误:(

4

3 回答 3

1
select distinct FirstName, LastName, table_2.status, CONCAT(FirstName, ' ', LastName) 
from table_1 inner join table_2 on table_1.key_2 = CONCAT('OFFLINE-', table_2.id) 
order by FirstName, LastName
于 2013-03-13T18:04:24.037 回答
0

You could do it with aliases instead of a subquery:

SELECT DISTINCT
    t2.FirstName, 
    t2.LastName, 
    t2.status, 
    CONCAT(t1.FirstName,' ', t1.LastName) as receiver
FROM table_1 t1
INNER JOIN 
    table_2 t2 on t1.key_2 = CONCAT('OFFLINE-', t2.id)
ORDER BY t1.FirstName, t1.LastName
于 2013-03-13T19:23:16.290 回答
0

这有效:)

SELECT DISTINCT
    FirstName, 
    LastName, 
    table_2.status, 
    (select CONCAT(FirstName,' ',LastName) as Name from table_1 where id=receiver) as receiver, 
FROM table_1
INNER JOIN 
    table_2 on table_1.key_2 = CONCAT('OFFLINE-', table_2.id)
ORDER BY FirstName, LastName
于 2013-03-13T19:11:02.197 回答