MySql - 选择记录(如果可用),否则选择下一个或上一个优先级记录

时间:2018-06-12 06:27:39

标签: mysql case

我有这个表结构

锻炼 ExercisesId | Name | PriorityWoman | PriorityMan | RefEquipment | MuscleBits

我从哪里选择优先明智的练习。我有1到9个优先级。

现在,我正在从表中选择某些优先级的数据,我想要做的是假设6优先级不可用,那么我想选择下一个最低(即优先级7/8/9)优先级表中的练习或之前的最高(即优先级为5/4/3/2/1)练习如果下一个没有......

这是我到目前为止所做的事情

SELECT Exercises.*,
       GymToEquipment.RefEquipment
FROM Exercises,
     GymToEquipment
WHERE GymToEquipment.RefGym = '29'
  AND GymToEquipment.RefEquipment = Exercises.RefEquipment
  AND (CASE
           WHEN Exercises.MuscleBits IN('Back hand',
                                        'Front hand',
                                        'Shoulders',
                                        'Back') THEN Exercises.PriorityWoman IN (1)
           WHEN Exercises.MuscleBits IN('Stomach') THEN Exercises.PriorityWoman IN (3)
           WHEN Exercises.MuscleBits IN('Chest') THEN Exercises.PriorityWoman IN (2)
           ELSE Exercises.PriorityWoman IN (7)
       END)

示例数据

  

CREATE TABLE练习(ExerciseId int(11)NOT NULL,Name varchar(255)   COLLATE utf8_unicode_ci NOT NULL,PriorityMan int(11)NOT NULL,   PriorityWoman int(11)NOT NULL,图像文本COLLATE utf8_unicode_ci NOT   NULL,RefEquipment int(11)DEFAULT NULL,MuscleBits文本COLLATE   utf8_unicode_ci);

     

插入

     

运动(ExerciseId,名称,PriorityMan,PriorityWoman,图像,RefEquipment,MuscleBits)   VALUES   (2,' E1',1,3,'本地主机/健身房/图像/ 1525077944_5ae6d7b85dab8.png',4,'腿&#39),   (3,' E2',2,6-,'本地主机/健身房/图像/ 1525079863_5ae6df371e6b7.png' 5,'腿&#39),   (4,' E3',3,7-,'本地主机/健身房/图像/ 1525079494_5ae6ddc68da4d.png',6,'腿&#39),(   5,' E4',2,6-,'本地主机/健身房/图像/ 1525079530_5ae6ddea877cf.png',4,'腿&#39),   (6,' E5',3,7-,'本地主机/健身房/图像/ 1525079559_5ae6de078149e.png',4,'腿&#39),   (7,' E6' 6,8-,'本地主机/健身房/图像/ 1525079627_5ae6de4b5cb8b.png' 9,'腿&#39),   (8,' E7',7,1,'本地主机/健身房/图像/ 1525079672_5ae6de7894667.png' 10,'腿&#39),   (9,' E8',8,2,'本地主机/健身房/图像/ 1525079702_5ae6de96126bf.png' 11,'腿&#39),   (10,' E9',9,4,'本地主机/健身房/图像/ 1525079750_5ae6dec61ea58.png' 12,'腿&#39),   (11,' E10',10,5,'本地主机/健身房/图像/ 1525079779_5ae6dee33bda9.png' 12,'腿&#39),   (13,' E11',1,1,'本地主机/健身房/图像/ 1525081036_5ae6e3cc869df.png' 14,'返回&#39),   (14,' E12',2,2,'本地主机/健身房/图像/ 1525081270_5ae6e4b6bd066.png' 31,'返回&#39),   (15,' E13',3,3,'本地主机/健身房/图像/ 1525081310_5ae6e4dea9479.png' 16,'返回&#39),   (16,' E14',4,4,'本地主机/健身房/图像/ 1525081375_5ae6e51f06e44.png' 17,'返回&#39),   (17,' E15',5,5,'本地主机/健身房/图像/ 1525081416_5ae6e548dca82.png' 14,'返回&#39),   (18' E16',6,6,'本地主机/健身房/图像/ 1525081466_5ae6e57a839b7.png' 14,'返回&#39),   (19' E17',7,7,'本地主机/健身房/图像/ 1525081507_5ae6e5a3c4861.png' 14,'返回&#39),   (20,' E19',8,8,'本地主机/健身房/图像/ 1525081532_5ae6e5bc8c703.png' 14,'返回&#39),   (21,' E20',9,9-,'本地主机/健身房/图像/ 1525081577_5ae6e5e9c7730.png' 44,'返回&#39),   (22' E21',10,10,'本地主机/健身房/图像/ 1525081618_5ae6e6125b122.png' 44,'返回&#39),   (23' E22',1,1,'本地主机/健身房/图像/ 1525081756_5ae6e69ce77cd.png' 24,'胸围&#39),   (24,' E23',2,2,'本地主机/健身房/图像/ 1525081789_5ae6e6bd3d1fe.png' 25,'胸围&#39),   (25,' E24',3,3,'本地主机/健身房/图像/ 1525081813_5ae6e6d55e80c.png' 25,'胸围&#39),   (26' E25',4,4,'本地主机/健身房/图像/ 1525081854_5ae6e6fe10c7d.png' 27,'胸围&#39),   (27' 26',5,5,'本地主机/健身房/图像/ 1525081905_5ae6e73158153.png' 28,'胸围&#39),   (28' 27',6,6,'本地主机/健身房/图像/ 1525081927_5ae6e74752a0e.png' 28,'胸围&#39),   (29' 28',1,2,'本地主机/健身房/图像/ 1525082103_5ae6e7f774930.png' 30,'肩部&#39),   (30' 29',2,1,'本地主机/健身房/图像/ 1525082069_5ae6e7d5382c6.png' 31,'肩部&#39),   (31' 30',3,3,'本地主机/健身房/图像/ 1525082168_5ae6e838e7091.png' 32,'肩部&#39),   (32,' 31',4,4,'本地主机/健身房/图像/ 1525082215_5ae6e867a7600.png' 32,'肩部&#39),   (33' 32',5,5,'本地主机/健身房/图像/ 1525082289_5ae6e8b16dc8c.png' 34,'肩部&#39),   (34' 33',1,1,'本地主机/健身房/图像/ 1525082321_5ae6e8d17cc8e.png' 35,'胃&#39),   (35' 34',2,2,'本地主机/健身房/图像/ 1525082347_5ae6e8ebc1f03.png' 36,'胃&#39),   (36' 35',3,3,'本地主机/健身房/图像/ 1525082367_5ae6e8ff6adb4.png' 36,'胃&#39),   (37' 36',1,1,'本地主机/健身房/图像/ 1525082436_5ae6e94408290.png' 40,'前   手&#39),   (38' 37',2,2,'本地主机/健身房/图像/ 1525082460_5ae6e95c6d067.png' 38,'前   手&#39),   (39' 38',3,3,'本地主机/健身房/图像/ 1525082483_5ae6e9733a0b5.png' 40,'前   手&#39),   (40' 39',1,3,'本地主机/健身房/图像/ 1525082533_5ae6e9a5a2b42.png' 41,'返回   手&#39),   (41' 40',2,1,'本地主机/健身房/图像/ 1525082574_5ae6e9cea82f3.png' 42,'返回   手&#39);

修改

有6个不同的muscle_bits Legs,Chest,...等。如果3肌肉位中没有优先级3,我选择一个优先级为(Legs, Chest)的组Legs然后为Legs肌肉位

选择另一个练习

预期输出(我稍后会格式化数据,我只需要从表中提取练习)

{"天":[{" DayNumber":0," TIMEPERIOD":0,"肌肉":[{&# 34; MuscleId":"腿"" Excercises":[{" Excerciseid":" 6"" DEVICEID& #34;:" 4"" ExcerciseSet":[{" Setnumber":1,"重复次数":30,"重量和#34;:56.55"间隔" 40},{" Setnumber":2"重复次数":30,"重量" :56.55"间隔" 40}]}]},{" MuscleId":"返回"" Excercises":[{& #34; Excerciseid":" 15"" DEVICEID":" 16"" ExcerciseSet":[{" Setnumber":1,"重复次数":30,"重量":60.9,"间隔" 40},{" Setnumber" :2,"重复次数":30,"重量":60.9,"间隔" 40}]}]},{" MuscleId" :"胸围"" Excercises":[{" Excerciseid":" 25"" DEVICEID":&# 34; 25"" ExcerciseSet":[{" Setnumber":1,"重复次数":30,"重量":60.9 "间隔&#34 ; 40},{" Setnumber":2"重复次数":30,"重量":60.9,"间隔" 40} ]}]},{" MuscleId":"肩部"" Excercises":[{" Excerciseid":" 31&# 34;," DEVICEID":" 32"" ExcerciseSet":[{" Setnumber":1,"重复次数&#34 ;:30,"重量":45.24"间隔" 40},{" Setnumber":2"重复次数":30, "重量":45.24"间隔" 40}]}]},{" MuscleId":"胃"" Excercises":[{" Excerciseid":" 36"" DEVICEID":" 36"" ExcerciseSet&#34 ;:[{" Setnumber":1,"重复次数":30,"重量":52.2,"间隔" 40},{ " Setnumber":2"重复次数":30,"重量":52.2,"间隔" 40}]}]},{ " MuscleId":"前手","练习":[{" Excerciseid":" 39",&# 34; DEVICEID":" 40"" ExcerciseSet":[{" Setnumber":1,"重复次数":30,& #34; W 8":43.935,"间隔" 40},{" Setnumber":2"重复次数":30,"重量" :43.935,"间隔" 40}]}]}]}]}

1 个答案:

答案 0 :(得分:1)

我不太清楚你的数据如何挂起,因为你还没有为GymToEquipment包含任何数据。您可以使用union(如果存在值,将为每个肌肉比特找到下一个最高优先级和下一个最低优先级)找到一些东西,并且限制为获得适当的值,例如

SET @LEGS = 3;SET @BACK = 7;

SELECT * FROM
(
SELECT 'LEG' AS EXCERCISE, S.* FROM
(
SELECT EXERCISEID,PRIORITYWOMAN FROM EXERCISES WHERE PRIORITYWOMAN = @LEGS AND MUSCLEBITS = 'LEGS' 
UNION 
SELECT EXERCISEID,PRIORITYWOMAN FROM EXERCISES 
    WHERE PRIORITYWOMAN  = (SELECT MIN(E1.PRIORITYWOMAN) FROM EXERCISES E1 WHERE E1.PRIORITYWOMAN > @LEGS AND MUSCLEBITS = 'LEGS' 
    LIMIT 1) 
UNION
SELECT EXERCISEID,PRIORITYWOMAN FROM EXERCISES 
 WHERE PRIORITYWOMAN  = (SELECT MAX(E1.PRIORITYWOMAN) FROM EXERCISES E1 WHERE E1.PRIORITYWOMAN < @LEGS AND MUSCLEBITS = 'LEGS' 
    ORDER BY PRIORITYWOMAN DESC LIMIT 1) 
UNION  
SELECT 9999,999 FROM DUAL
) S
LIMIT 1
) A
UNION
(SELECT * FROM
(
SELECT 'BACK' AS EXERCISE, S.* FROM
(
SELECT EXERCISEID,PRIORITYWOMAN FROM EXERCISES WHERE PRIORITYWOMAN = @BACK AND MUSCLEBITS = 'BACK' 
UNION 
SELECT EXERCISEID,PRIORITYWOMAN FROM EXERCISES 
    WHERE PRIORITYWOMAN  = (SELECT MIN(E1.PRIORITYWOMAN) FROM EXERCISES E1 WHERE E1.PRIORITYWOMAN > @BACK AND MUSCLEBITS = 'BACK' 
    LIMIT 1) 
UNION
SELECT EXERCISEID,PRIORITYWOMAN FROM EXERCISES 
 WHERE PRIORITYWOMAN  = (SELECT MAX(E1.PRIORITYWOMAN) FROM EXERCISES E1 WHERE E1.PRIORITYWOMAN < @BACK AND MUSCLEBITS = 'BACK' 
    ORDER BY PRIORITYWOMAN DESC LIMIT 1) 
UNION  
SELECT 9999,999 FROM DUAL
) S
LIMIT 1
) B
) 

结果

+-----------+------------+---------------+
| EXCERCISE | EXERCISEID | PRIORITYWOMAN |
+-----------+------------+---------------+
| LEG       |          2 |             3 |
| BACK      |         19 |             7 |
+-----------+------------+---------------+
2 rows in set (0.04 sec)

是的,我知道它太可怕了。