mysql:连接结构中的顺序子句写在哪里?

发布于 2024-09-25 07:08:08 字数 1361 浏览 0 评论 0原文

假设我有三个表 - listab

table list
|a_id|--|b_id|--'name'
|4   |--|3   |--|foo|
|1   |--|2   |--|foo|

table a
|id|--|name|--|order|
|1 |--|a_1 |--|1    |
|2 |--|a_2 |--|2    |
.....................
|n |--|a_n |--|n    |

table b
|id|--|name|--|order|
|1 |--|b_1 |--|1    |
|2 |--|b_2 |--|2    |
.....................
|n |--|b_n |--|n    |

a_idlist > 是来自 a 表的 id,

list 中的 b_id 是来自 b 表的 id

我需要获取ab 表中的名称列表按其顺序排序,其中 list 表中的 name = 'foo'。

IE。我需要从表 ab_2b_3 中获取 a_1a_4来自b

我编写了一个类似的查询,

SELECT
                    `a_1_table`.`name` a_1_name,
                    `b_1_table`.`name` b_1_name
                FROM
                    `list`
                LEFT JOIN 
                    `a_1` AS a_1_table ON ( `a_1_table`.`id` = `list`.`a_id` )
                LEFT JOIN 
                    `b_1` AS b_1_table ON ( `b_1_table`.`id` = `list`.`b_id` )
                WHERE 
                    `list`.`name` = 'foo'

但正如我所见,在这种结构中,我无法编写 order by ...。 IE。我可以,但我不会得到我想要的。

我能做些什么?

Let's assume I have three table - list,a and b

table list
|a_id|--|b_id|--'name'
|4   |--|3   |--|foo|
|1   |--|2   |--|foo|

table a
|id|--|name|--|order|
|1 |--|a_1 |--|1    |
|2 |--|a_2 |--|2    |
.....................
|n |--|a_n |--|n    |

table b
|id|--|name|--|order|
|1 |--|b_1 |--|1    |
|2 |--|b_2 |--|2    |
.....................
|n |--|b_n |--|n    |

a_id in table list is id from a table, and

b_id in table list is id from b table

I need to get the name list from a and b tables ordered by their order, where name = 'foo' in list table.

ie. i need to get a_1,a_4 from table a, and b_2,b_3 from b.

I wrote a query like

SELECT
                    `a_1_table`.`name` a_1_name,
                    `b_1_table`.`name` b_1_name
                FROM
                    `list`
                LEFT JOIN 
                    `a_1` AS a_1_table ON ( `a_1_table`.`id` = `list`.`a_id` )
                LEFT JOIN 
                    `b_1` AS b_1_table ON ( `b_1_table`.`id` = `list`.`b_id` )
                WHERE 
                    `list`.`name` = 'foo'

but as I see, in such structure, I can't write order by .... ie. I can' but I will not get what I want.

What can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

冷情 2024-10-02 07:08:08

使用:

SELECT x.name
    FROM (SELECT a.name, a.order, 1 AS sort
            FROM LIST l
            JOIN a ON a.id = list.a_id
          WHERE l.name = 'foo'
          UNION ALL
          SELECT b.name, b.order, 2 AS sort
            FROM LIST l
            JOIN b ON b.id = list.b_id
           WHERE l.name = 'foo') x
ORDER BY x.sort, x.order

UNION ALLUNION 更快,因为它不会删除重复项(UNION 会删除重复项)。

Use:

SELECT x.name
    FROM (SELECT a.name, a.order, 1 AS sort
            FROM LIST l
            JOIN a ON a.id = list.a_id
          WHERE l.name = 'foo'
          UNION ALL
          SELECT b.name, b.order, 2 AS sort
            FROM LIST l
            JOIN b ON b.id = list.b_id
           WHERE l.name = 'foo') x
ORDER BY x.sort, x.order

UNION ALL is faster than UNION, because it doesn't remove duplicates (which UNION does).

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