MySQL:按另一个表中的行数排序?

发布于 2024-11-25 06:30:30 字数 818 浏览 2 评论 0原文

SELECT e.*,
(
    SELECT COUNT(*) FROM
    (
        SELECT tm.exerciseID FROM exercise_targetedmuscles tm
        WHERE tm.exerciseID = e.exerciseID
        AND tm.isPrimary = 1
        AND tm.targetedMuscleID IN (4,11)
    ) as musclesCovered
) as numMusclesCovered
FROM exercises e
ORDER BY numMusclesCovered DESC

基本上,我想按照练习所覆盖的目标肌肉数量对练习进行排序(在示例中,目标肌肉是 4 块和 11 块)。但是,子查询(出于某种原因?)不知道 e 是什么,所以它不起作用。

关于如何让这个查询正确排序我的结果有什么想法吗?谢谢!

编辑:在兰迪的有用评论后想出了这个。

SELECT COUNT(tm.targetedMuscleID) as numMusclesCovered, e.*, tm.* FROM exercise_targetedmuscles tm

JOIN exercises e ON tm.exerciseID = e.exerciseID

WHERE tm.isPrimary = 1
AND tm.targetedMuscleID IN (4,11)

GROUP BY tm.exerciseID

ORDER BY numMusclesCovered DESC

非常感谢!

SELECT e.*,
(
    SELECT COUNT(*) FROM
    (
        SELECT tm.exerciseID FROM exercise_targetedmuscles tm
        WHERE tm.exerciseID = e.exerciseID
        AND tm.isPrimary = 1
        AND tm.targetedMuscleID IN (4,11)
    ) as musclesCovered
) as numMusclesCovered
FROM exercises e
ORDER BY numMusclesCovered DESC

Basically, I want to order the exercises by the number of targetted muscles they cover (in the example, the targetted muscles are 4 and 11). However, the subquery (for some reason?) doesn't know what e is, so it does not work.

Any ideas as to how I can get this query to order my results properly? Thanks!

EDIT: came up with this after Randy's helpful comment.

SELECT COUNT(tm.targetedMuscleID) as numMusclesCovered, e.*, tm.* FROM exercise_targetedmuscles tm

JOIN exercises e ON tm.exerciseID = e.exerciseID

WHERE tm.isPrimary = 1
AND tm.targetedMuscleID IN (4,11)

GROUP BY tm.exerciseID

ORDER BY numMusclesCovered DESC

thanks so much!

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

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

发布评论

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

评论(3

雄赳赳气昂昂 2024-12-02 06:30:30

了解表格的结构可能会有所帮助。我建议一个与此类似的解决方案。

SELECT e.*, tm.*, COUNT(tm.targetedMuscleID) AS coveredMuscles
FROM exercises AS e
JOIN exercise_targetedmuscles AS tm ON tm.exerciseID = e.exerciseID
WHERE tm.isPrimary = 1 AND tm.targetedMuscleID IN (4, 11)
GROUP BY e.exerciseID
ORDER BY coveredMuscles;

Knowing how the tables are structured might be helpful. I suggest a solution similar to this.

SELECT e.*, tm.*, COUNT(tm.targetedMuscleID) AS coveredMuscles
FROM exercises AS e
JOIN exercise_targetedmuscles AS tm ON tm.exerciseID = e.exerciseID
WHERE tm.isPrimary = 1 AND tm.targetedMuscleID IN (4, 11)
GROUP BY e.exerciseID
ORDER BY coveredMuscles;
壹場煙雨 2024-12-02 06:30:30

如果我理解正确的话,你根本不需要子查询

SELECT count(tm.targetedMuscleID) as num_muscles, e.exerciseID 
FROM exercise_targetedmuscles tm, exercises e
WHERE tm.exerciseID = e.exerciseID
AND tm.isPrimary = 1
AND tm.targetedMuscleID IN (4,11)
GROUP BY e.exerciseID
order by num_muscles

If I understand correctly, you don't need subquery at all

SELECT count(tm.targetedMuscleID) as num_muscles, e.exerciseID 
FROM exercise_targetedmuscles tm, exercises e
WHERE tm.exerciseID = e.exerciseID
AND tm.isPrimary = 1
AND tm.targetedMuscleID IN (4,11)
GROUP BY e.exerciseID
order by num_muscles
自找没趣 2024-12-02 06:30:30

MySQL 中的 SQL 子查询有类似“本地命名空间”的东西。我没有能力进行测试,但您可能正在搜索类似这样的内容:

SELECT `data`.* FROM (
   SELECT COUNT(tm.targetedMuscleID) as `count`,tm.targetedMuscleID FROM exercise_targetedmuscles tm
   LEFT JOIN `exercises` as `e` ON (`tm`.`exerciseID`=`e`.`exerciseID`)
   WHERE
       tm.isPrimary = 1
   AND
       tm.targetedMuscleID IN (4,11)
   GROUP BY tm.targetedMuscleID
) as `data`
ORDER BY `data`.`count`

但是,在某些情况下,您根本不必使用子查询。

SQL subqueries in MySQL has something like "local namespace". I haven`t got ability to test, but you may be searching for something like this:

SELECT `data`.* FROM (
   SELECT COUNT(tm.targetedMuscleID) as `count`,tm.targetedMuscleID FROM exercise_targetedmuscles tm
   LEFT JOIN `exercises` as `e` ON (`tm`.`exerciseID`=`e`.`exerciseID`)
   WHERE
       tm.isPrimary = 1
   AND
       tm.targetedMuscleID IN (4,11)
   GROUP BY tm.targetedMuscleID
) as `data`
ORDER BY `data`.`count`

But, in some cases, you do not have to use subquery at all.

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