复杂的 MySQL 连接

发布于 2024-12-12 06:33:23 字数 928 浏览 0 评论 0原文

我有以下 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_deletedtrk_deleted,如果已删除(从视图中隐藏),则可以具有 1 值,如果未删除,则可以具有 0 值(仍在视野中)。

我的项目视图页面根据每个项目中的轨迹线显示该项目的图标。这些图标根据 trk_statustrk_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 技术交流群。

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

发布评论

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

评论(2

甜尕妞 2024-12-19 06:33:23

如果您可以提供一些示例数据以及您想要的响应内容,我可能可以帮助您进行更好的查询。同时:

SELECT Project.*, MAX(`trk_deleted`), MAX(`trk_status`), MAX(`trk_state`)
FROM `project` AS `Project` 
LEFT JOIN `trackline` ON `prj_id` = `trk_project` AND trk_deleted = '0'
WHERE `prj_deleted` = '0'
GROUP BY `prj_id` ORDER BY `prj_name`;

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:

SELECT Project.*, MAX(`trk_deleted`), MAX(`trk_status`), MAX(`trk_state`)
FROM `project` AS `Project` 
LEFT JOIN `trackline` ON `prj_id` = `trk_project` AND trk_deleted = '0'
WHERE `prj_deleted` = '0'
GROUP BY `prj_id` ORDER BY `prj_name`;
他不在意 2024-12-19 06:33:23

如果我没理解错的话,您想要获取所有未删除的项目,无论是否有轨迹线,以及未删除的轨迹线。但添加 trk_delete = 0 会排除没有轨迹线的项目。

我对 mysql 不太熟悉,但我认为你想要的是 IS NULL

SELECT 
    Project.*, 
    `trk_deleted`, 
    `trk_status`, 
    `trk_state` 
FROM 
    `project` AS `Project` 
LEFT JOIN 
    `trackline` ON `prj_id` = `trk_project` 
WHERE 
    `prj_deleted` = '0' 
    AND trk_deleted = '0' OR trk_deleted IS NULL
GROUP BY 
    `prj_id` 
ORDER BY 
    `prj_name`

如果不是这样,请澄清一下

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

SELECT 
    Project.*, 
    `trk_deleted`, 
    `trk_status`, 
    `trk_state` 
FROM 
    `project` AS `Project` 
LEFT JOIN 
    `trackline` ON `prj_id` = `trk_project` 
WHERE 
    `prj_deleted` = '0' 
    AND trk_deleted = '0' OR trk_deleted IS NULL
GROUP BY 
    `prj_id` 
ORDER BY 
    `prj_name`

If that's not it, please clarify yourself

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