0

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

我想从所有 4 个表中获得共同值:“wins”,其中“account”=1,然后按大多数“wins”排序。并且所有订购的“获胜” LIMIT 为 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

但这确实是错误的。

4

2 回答 2

0

如果您需要知道从中获得结果的表:

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
于 2013-08-28T15:49:51.720 回答
0

选择 o.* 从A
JOIN B
JOIN C
JOIN D
ON Daccount= Aaccount
Caccount= Aaccount
Baccount= Aaccount
Aaccount='1'
订购方式Awins

于 2013-08-28T15:50:33.093 回答