我正在尝试加入5个不同的表格。他们每人都有一个
名称列,Id列和Exp列
除此之外,所有这些都没有完全相同。
SELECT name FROM `weapons`,`arrows`,`trees`,`ores`,`bars` ORDER BY exp DESC
我是否可以提出,其中名称含糊不清。谢谢!
这就像我试图合并为一个大列表的5个单独的用户表
答案 0 :(得分:1)
如果您具有相同名称的列,则需要将表名添加到列中。您还可以使用表的别名并缩短查询。
如果您想选择name
和weapons
表的arrows
列,请执行
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
答案 1 :(得分: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 tables
那么:
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;
答案 2 :(得分:0)
如果你正在寻找一个大的列表,你想要UNION表,而不是JOIN。
SELECT * FROM weapons
UNION
SELECT * FROM arrows
UNION
SELECT * FROM trees
UNION
SELECT * FROM ores
UNION
SELECT * FROM bars;
如果您只想要名称SELECT name
而不是SELECT *
答案 3 :(得分: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
我添加了一个最后一列,以帮助确定数据来自哪个表。如果您不想这样,那么您可以放弃它。