mysql select查询帮助--ORDER BY
我正在尝试构建一个选择查询,它将获取 4 个表的所有列,然后按“名称”列排序并显示结果(所有表中都相同)。我仍在学习 MySQL 的诀窍。
我发现由于列共享名称“name”,因此仅显示最后一个表的结果。有没有一种方法可以执行此查询并保留所有表中的所有数据?
我应该使用不同的列名称吗?共享一个名称似乎更容易,因为每个名称中的信息都是相同的。
SELECT * FROM table_one, table_two, table_three, table_four ORDER BY...
四个表没有连接,结构不同......一些列名是共享的(看起来我应该修复它,我现在仍然可以),但每个表都有不同数量的列。
谢谢你!
I'm trying to construct a select query where it will take all the columns of 4 tables and then order and display the results by the column 'name' (the same in all tables). I'm still learning the ropes of MySQL.
I'm finding that because the columns share the name 'name', only the results from the last table are displayed. Is there a way of performing this query which retains all the data from all the tables?
Should I use different column names? It seems easier to share one name since it is the same information in each.
SELECT * FROM table_one, table_two, table_three, table_four ORDER BY...
The four tables not joined, and the structure is different... Some column names are shared (which it looks like I should fix, I still can at this point), but each has a different amount of columns.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果四个表之间没有关系,请使用
UNION
代替:这里有两个选项 -
UNION
速度较慢,因为它会删除重复项 - 最终列表将是唯一的名单。UNION ALL
速度更快,因为它不会删除重复项。要从表中获取列,请使用:
If there's no relationship between the four tables, use
UNION
s instead:There's two options here -
UNION
is slower, because it will remove duplicates - the final list will be a unique list of names.UNION ALL
is faster because it doesn't remove duplicates.To get the columns from the tables as well, use:
是的,您应该使用不同的列名称,但是要获取所有数据,您也可以编写如下查询:
Yes you should use different column names, but to get all data also you can write quesries like this:
johnny_n,
您应该使用
显然您需要使用正确的语法,但使用 AS 关键字将允许您在查询中使用所需的键。
johnny_n,
You should use
Obviously you need to use the correct syntax, but using the AS keyword, will allow you to use the key you want in your query.
如果他们都有相同的
名字
......If they all share the same
name
's....