我想做一个MySQL查询。我有4张桌子:A,B,C,D。

我想得到一个共同的价值:从“帐户” = 1的所有4个表中“获胜”,然后按大多数“获胜”排序。并且所有订购的“获胜”都以3为限。

如果您能帮助我,我将非常高兴。

我尝试过这样的事情:

SELECT o.*
FROM (
(SELECT wins FROM 'A')
UNION ALL
(SELECT wins FROM 'B')
UNION ALL
(SELECT wins FROM 'C')
UNION ALL
(SELECT wins FROM 'D')
UNION ALL
(SELECT * FROM 'A'
WHERE
    account=1)
) AS o
ORDER BY wins DESC


但这确实是错误的。

最佳答案

如果您需要知道从中获得结果的表,请执行以下操作:

SELECT *
FROM (  SELECT wins, 'Table A' AS DataTable
        FROM TableA
        WHERE account = 1
        UNION ALL
        SELECT wins, 'Table B'
        FROM TableB
        WHERE account = 1
        UNION ALL
        SELECT wins, 'Table C'
        FROM TableC
        WHERE account = 1
        UNION ALL
        SELECT wins, 'Table D'
        FROM TableD
        WHERE account = 1) AS X
ORDER BY wins
LIMIT 3


如果那并不重要,那么您只需要最高的wins即可:

SELECT *
FROM (  SELECT wins
        FROM TableA
        WHERE account = 1
        UNION
        SELECT wins
        FROM TableB
        WHERE account = 1
        UNION
        SELECT wins
        FROM TableC
        WHERE account = 1
        UNION
        SELECT wins
        FROM TableD
        WHERE account = 1) AS X
ORDER BY wins DESC
LIMIT 3

09-30 22:54