mysql select查询帮助--ORDER BY

发布于 2024-08-22 14:27:22 字数 372 浏览 6 评论 0原文

我正在尝试构建一个选择查询,它将获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼前雾蒙蒙 2024-08-29 14:27:22

如果四个表之间没有关系,请使用 UNION 代替:

SELECT a.name
  FROM TABLE_ONE a
UNION
SELECT b.name
  FROM TABLE_TWO b
UNION
SELECT c.name
  FROM TABLE_THREE c
UNION
SELECT d.name
  FROM TABLE_FOUR d
ORDER BY name

这里有两个选项 - UNION 速度较慢,因为它会删除重复项 - 最终列表将是唯一的名单。 UNION ALL 速度更快,因为它不会删除重复项。

要从表中获取列,请使用:

SELECT a.*,
       b.*,
       c.*,
       d.*
  FROM (SELECT a.name
          FROM TABLE_ONE a
        UNION
        SELECT b.name
          FROM TABLE_TWO b
        UNION
        SELECT c.name
          FROM TABLE_THREE c
        UNION
        SELECT d.name
          FROM TABLE_FOUR d) x
LEFT JOIN TABLE_ONE a ON a.name = x.name
LEFT JOIN TABLE_TWO b ON b.name = x.name
LEFT JOIN TABLE_THREE c ON c.name = x.name
LEFT JOIN TABLE_FOUR d ON d.name = x.name

If there's no relationship between the four tables, use UNIONs instead:

SELECT a.name
  FROM TABLE_ONE a
UNION
SELECT b.name
  FROM TABLE_TWO b
UNION
SELECT c.name
  FROM TABLE_THREE c
UNION
SELECT d.name
  FROM TABLE_FOUR d
ORDER BY name

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:

SELECT a.*,
       b.*,
       c.*,
       d.*
  FROM (SELECT a.name
          FROM TABLE_ONE a
        UNION
        SELECT b.name
          FROM TABLE_TWO b
        UNION
        SELECT c.name
          FROM TABLE_THREE c
        UNION
        SELECT d.name
          FROM TABLE_FOUR d) x
LEFT JOIN TABLE_ONE a ON a.name = x.name
LEFT JOIN TABLE_TWO b ON b.name = x.name
LEFT JOIN TABLE_THREE c ON c.name = x.name
LEFT JOIN TABLE_FOUR d ON d.name = x.name
停顿的约定 2024-08-29 14:27:22

是的,您应该使用不同的列名称,但是要获取所有数据,您也可以编写如下查询:

SELECT table_one.* t1, table_two.* t2, table_three.* t3, table_four.* t4 FROM table_one, table_two, table_three, table_four ORDER BY...

Yes you should use different column names, but to get all data also you can write quesries like this:

SELECT table_one.* t1, table_two.* t2, table_three.* t3, table_four.* t4 FROM table_one, table_two, table_three, table_four ORDER BY...
木格 2024-08-29 14:27:22

johnny_n,

您应该使用

 SELECT name as name1, name as name2, name as name3 etc...

显然您需要使用正确的语法,但使用 AS 关键字将允许您在查询中使用所需的键。

johnny_n,

You should use

 SELECT name as name1, name as name2, name as name3 etc...

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.

破晓 2024-08-29 14:27:22

如果他们都有相同的名字......

SELECT * 
    FROM table_one 
LEFT JOIN
    table_two USING(name)
LEFT JOIN
    table_three USING(name)
LEFT JOIN
    table_four USING(name)
ORDER BY name

If they all share the same name's....

SELECT * 
    FROM table_one 
LEFT JOIN
    table_two USING(name)
LEFT JOIN
    table_three USING(name)
LEFT JOIN
    table_four USING(name)
ORDER BY name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文