复杂的 MySQL 连接
我有以下 MySQL 查询:
SELECT Project.*, `trk_deleted`, `trk_status`, `trk_state`
FROM `project` AS `Project` LEFT JOIN `trackline` ON `prj_id` = `trk_project`
WHERE `prj_deleted` = '0' GROUP BY `prj_id` ORDER BY `prj_name`;
这有点难以解释,但基本上,我的数据库中有一个结构,其中项目有多个轨迹线。这两个实体分别具有字段 prj_deleted
和 trk_deleted
,如果已删除(从视图中隐藏),则可以具有 1 值,如果未删除,则可以具有 0 值(仍在视野中)。
我的项目视图页面根据每个项目中的轨迹线显示该项目的图标。这些图标根据 trk_status
和 trk_state
的值显示。
我的项目视图页面应该只显示该项目 1 次,即使它有多个轨迹线(因此 GROUP BY < code>prj_id)
我的问题:
来自 GROUP BY 上的 Mysql 文档:
“此外,添加 ORDER BY 子句不会影响从每个组中选择值。选择值后会对结果集进行排序,并且 ORDER BY 不会影响服务器选择哪些值。”
理想情况下,我的项目视图页面应该显示未删除的轨迹线的图标。这就是为什么我希望在 GROUP BY 发生之前可以 ORDER BY trk_deleted
,以便使用未删除的轨迹线完成分组。
希望有人能理解我在这里想要表达的意思。
I have the following MySQL query:
SELECT Project.*, `trk_deleted`, `trk_status`, `trk_state`
FROM `project` AS `Project` LEFT JOIN `trackline` ON `prj_id` = `trk_project`
WHERE `prj_deleted` = '0' GROUP BY `prj_id` ORDER BY `prj_name`;
This is a bit hard to explain, but basically, I have a structure in my database where Projects have multiple Tracklines. Both of these entities have fields prj_deleted
and trk_deleted
respectively which can have a 1 value if it was deleted (hidden from view), or a 0 value is it is not deleted (still in view).
My Project view page shows icons for each project based on the tracklines in that project. These icons are displayed based on the values of trk_status
and trk_state
My Project view page should only show the project 1 time, even if it has multiple tracklines (hence the GROUP BY prj_id
)
My Problem:
From the Mysql Documentation on GROUP BY:
"Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause. Sorting of the result set occurs after values have been chosen, and ORDER BY does not affect which values the server chooses."
My Project view page should ideally be displaying the icons for the tracklines that are not deleted. This is why I was hoping I could ORDER BY trk_deleted
before the GROUP BY occurred so that grouping was done with a trackline that wasn't deleted.
Hopefully someone can understand what I'm trying to get at here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您可以提供一些示例数据以及您想要的响应内容,我可能可以帮助您进行更好的查询。同时:
If you could provide some sample data and what you would like for the response, I could probably help with a better query. In the mean time:
如果我没理解错的话,您想要获取所有未删除的项目,无论是否有轨迹线,以及未删除的轨迹线。但添加
trk_delete = 0
会排除没有轨迹线的项目。我对 mysql 不太熟悉,但我认为你想要的是 IS NULL
如果不是这样,请澄清一下
If I got you right, you want to get all projects that are not deleted, having tracklines or not, and the tracklines that are not deleted. But adding
trk_delete = 0
excludes the projects that do not have tracklines.I'm not very familiar with mysql, but i think that you want is IS NULL
If that's not it, please clarify yourself