gpt4 book ai didi

mysql - 具有嵌套连接和选择的 SQL 查询

转载 作者:太空宇宙 更新时间:2023-11-03 12:36:15 27 4
gpt4 key购买 nike

我正在尝试从我的数据库中的菜单项列表中将多层菜单放在一起。这是我的查询。 fl=一级,sl=二级,以此类推。

这是查询:

SELECT name as flName,id AS flId
FROM menu_items AS m1
WHERE parent_id = 0
INNER JOIN (SELECT name AS slName, id AS slId, parent_id AS slPid FROM menu_items)
m2 ON m1.firstLevelId = m2.parent_id
INNER JOIN (SELECT name AS tlName, id AS tlId, parent_id AS tlPid FROM menu_items)
m3 ON m2.id = m3.parent_id

我得到的错误是这样的:

PersistenceException: Query threw SQLException:You have an error in your SQL syntax.  
check the manual that corresponds to your MySQL server version for the right syntax to
use near 'INNER JOIN (SELECT name AS slName, id AS slId, parent_id AS slPid FROM
menu_item'

但它对我来说似乎有效,我不知道.. 很确定我以前在子查询上做过连接。我做错了什么吗?

最佳答案

将 where 子句移到末尾,如下所示:

SELECT name as flName,id AS flId
FROM menu_items AS m1 INNER JOIN (
SELECT name AS slName, id AS slId, parent_id AS slPid FROM menu_items
) m2 ON m1.firstLevelId = m2.parent_id
INNER JOIN (
SELECT name AS tlName, id AS tlId, parent_id AS tlPid FROM menu_items
) m3 ON m2.id = m3.parent_id
WHERE parent_id = 0

关于mysql - 具有嵌套连接和选择的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12983630/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com