1

我尝试从两个表中选择值:


    SELECT id, alife, login
    FROM players
    UNION ALL
    SELECT id, alife, (
        SELECT players.login
        FROM players
        WHERE players.id = id
        LIMIT 0 , 1
    )
    FROM attempts
    ORDER BY `alife`
    LIMIT 0 , 30

如何用表格线标记每一行?如

ID ALIFE 登录表
1 100 名 Any_A 玩家
4 90 名 Any_B 玩家
3 80 次 Any_C 尝试

谢谢

4

2 回答 2

4
 SELECT id, alife, login, 'players' as `TABLE`
    FROM players
    UNION ALL
    SELECT id, alife, (
        SELECT players.login
        FROM players
        WHERE players.id = id
        LIMIT 0 , 1
    ), 'attempts' as `TABLE`
    FROM attempts
    ORDER BY `alife`
    LIMIT 0 , 30
于 2013-09-09T17:29:13.420 回答
0

这是为了注意您的查询不正确。该行:

WHERE players.id = id

被解释为:

WHERE players.id = players.id

基于 SQL 范围规则。

大概你想要以下内容:

SELECT id, alife, login, 'players' as which
FROM players
UNION ALL
SELECT id, alife, (SELECT players.login
                   FROM players
                   WHERE players.id = attempts.player_id
                   LIMIT 0 , 1
                  ), 'attempts' as which
FROM attempts
ORDER BY `alife`
LIMIT 0 , 30;

此外,由于子查询没有order by并且关系为 1-1 是有意义的,因此将其id表述为连接会更清楚:

SELECT id, alife, login, 'players' as which
FROM players
UNION ALL
SELECT a.id, a.alife, p.login, 'attempts' as which
FROM attempts a left outer join
     players p
     on p.id = a.player_id
ORDER BY `alife`
LIMIT 0 , 30;

如果您正在处理大量数据,则可以进行其他修改以提高性能。

于 2013-09-09T17:51:55.677 回答