1

我正在尝试加入 5 个不同的表。他们每个人都有一个

Name 列、Id 列和 Exp 列

除此之外,他们没有什么完全一样的。

SELECT name FROM `weapons`,`arrows`,`trees`,`ores`,`bars` ORDER BY exp DESC

是我能想到的,它说名字是模棱两可的。谢谢!

这就像我试图将 5 个单独的用户表组合成一个大列表

4

4 回答 4

1

如果您有同名的列,则需要将表名添加到列中。您还可以为表使用别名并缩短查询。

如果要选择and表的name列,请执行weaponsarrows

SELECT w.name, a.name
FROM `weapons` as w, `arrows` as a,`trees` as t,`ores` ass o,`bars` as b
ORDER BY exp DESC
于 2012-10-24T17:48:49.257 回答
0

您可能希望使用一个表前缀来标识要从中检索的表,name例如:

 SELECT w.name FROM weapons w, arrows a, trees t, ores o, bars b ORDER BY exp DESC

我认为您也缺少加入条件。

您的查询应如下所示:

SELECT w.name 
FROM weapons w JOIN arrows a
     ON w.id = a.id
     JOIN trees t
     ON a.id = t.id
     JOIN ores o 
     ON t.id = o.id
     JOIN  bars b 
     ON o.id = b.id
ORDER BY exp DESC;

或者,如果你想要一个union --> combine rows from all tablesthen :

 SELECT name 
 FROM weapons 
 UNION ALL
 SELECT name 
 FROM arrows
 UNION ALL
 SELECT name 
 FROM trees
 UNION ALL
 SELECT name 
 FROM ores
 UNION ALL
 SELECT name 
 FROM bars;
于 2012-10-24T17:49:49.260 回答
0

如果您正在寻找一个大列表,您希望联合这些表,而不是 JOIN。

SELECT * FROM weapons
UNION
SELECT * FROM arrows
UNION 
SELECT * FROM trees
UNION 
SELECT * FROM ores
UNION
SELECT * FROM bars;

如果你只想要名字,SELECT name而不是SELECT *

于 2012-10-24T17:53:35.563 回答
0

如果您想要一个大列表,可以使用以下内容:

select name, id, exp, 'weapons' as TableFrom
from weapons
union all
select name, id, exp, 'arrows' as TableFrom
from arrows
union all
select name, id, exp, 'trees' as TableFrom
from trees
union all
select name, id, exp, 'ores' as TableFrom
from ores
union all
select name, id, exp, 'bars' as TableFrom
from bars
order by 3

我添加了最后一列来帮助确定数据来自哪个表。如果你不想这样,那么你可以放弃它。

于 2012-10-24T17:54:57.513 回答