MySQL-基于多态表值的条件查询结果?

时间:2019-05-21 04:09:03

标签: mysql sql polymorphism

我有一个多态表creationables,无法找出条件查询的关键部分,该条件查询基于creationables字段值(creationable_type返回所有humanrobot),具体取决于是否已将它们从各自的表(humansrobots,以及robotsrobot_upgrades组合中删除)中。这是4个表,后面是伪查询:

1。 creationables

| id | creationable_type | creationable_id | robot_upgrade_id | is_activated
----------------------------------------------------------------------------
| 1  |      human        |       1         |         0        |      1
| 2  |      robot        |       1         |         0        |      1
| 3  |      robot        |       2         |         1        |      1

2。人类

| id | name | deleted
---------------------
| 1  | Adam |  NULL

3。机器人

| id |  name   | deleted
------------------------
| 1  | Droid X |  NULL
| 2  | Droid Y |  NULL

4。 robot_upgrades

| id |  upgrade_name   | deleted
--------------------------------
| 1  |    Patch V4     |  NOW()

伪查询:

SELECT *
FROM creationables

 // If creationable_type == 'human'
 // we want to get non deleted humans
    JOIN humans ON humans.id=creationable_id WHERE humans.deleted=NULL

 // If creationable_type == 'robot' and robot_upgrade_id == '0'
 // we want to get non deleted robots
    JOIN robots ON robots.id=creationable_id WHERE robots.deleted=NULL

 // If creationable_type == 'robot' and robot_upgrade_id != '0'
 // we want to check both robots and robot_upgrades tables
 // and if either of them was deleted we do not want to return them
 // we want to get non deleted robots/robot_upgrades combo
    JOIN robots ON robots.id=creationable_id WHERE robots.deleted=NULL
    JOIN robot_upgrades ON robot_upgrades.id=robot_upgrade_id WHERE robot_upgrades.deleted=NULL

WHERE creationables.is_activated = '1'

您知道基于伪查询中的注释正确的条件查询是什么吗?

更新 这是fiddle

预期结果应如下所示:

| id | creationable_type | creationable_id | robot_upgrade_id | is_activated
----------------------------------------------------------------------------
| 1  |      human        |       1         |         0        |      1
| 2  |      robot        |       1         |         0        |      1

creationables行ID为3的行,因为即使未从robots中删除其机械手,其相关的robot_upgrade_id 1也已从robot_upgrades中删除。

1 个答案:

答案 0 :(得分:1)

没有直接的方法可以做到。 INNER JOINS会在第一个JOIN之后立即排除前面的类型,因此您可以使用LEFT JOINS执行以下操作:

SELECT *
FROM creationables A
    LEFT JOIN humans ON humans.id= A.creationable_id AND A.creationable_type = 'human' AND humans.deleted is NULL
    LEFT JOIN robots ON robots.id= A.creationable_id AND A.creationable_type = 'robot' AND robots.deleted is NULL
    LEFT JOIN robot_upgrades ON robot_upgrades.id=A.robot_upgrade_id AND A.creationable_type = 'robot' and robot_upgrade_id != '0' AND robot_upgrades.deleted is NULL
WHERE creationables.is_activated = '1';

或者您可以尝试像这样的UNION方法:

SELECT * FROM creationables A JOIN humans ON humans.id= A.creationable_id AND A.creationable_type = 'human' AND humans.deleted is NULL
  UNION
SELECT * FROM creationables A JOIN robots ON robots.id= A.creationable_id AND A.creationable_type = 'robot' AND robots.deleted is NULL
  UNION
SELECT * FROM creationables A JOIN robot_upgrades ON robot_upgrades.id=A.robot_upgrade_id AND A.creationable_type = 'robot' and robot_upgrade_id != '0' AND robot_upgrades.deleted is NULL
WHERE creationables.is_activated = '1';

如果删除了机械手更新,则它也不应该返回机械手,那么您可以改用以下查询:

SELECT A.creationable_type, humans.name, null as "update" 
FROM creationables A 
JOIN humans ON humans.id= A.creationable_id AND A.creationable_type = 'human' AND humans.deleted is NULL
  UNION
SELECT A.creationable_type, robots.name, robot_upgrades.upgrade_name as "update"
FROM creationables A 
JOIN robots ON robots.id= A.creationable_id AND A.creationable_type = 'robot' AND robots.deleted is NULL
LEFT JOIN robot_upgrades ON robot_upgrades.id=A.robot_upgrade_id AND A.creationable_type = 'robot' and robot_upgrade_id != '0' AND robot_upgrades.deleted is NULL
WHERE A.is_activated = '1' AND robots.id NOT IN
(SELECT DISTINCT A.creationable_id FROM creationables A
JOIN robot_upgrades ON robot_upgrades.id=A.robot_upgrade_id AND A.creationable_type = 'robot' and robot_upgrade_id != '0' AND robot_upgrades.deleted is NOT NULL);

这些可能不是最好的方法,但是可以完成工作,希望这会有所帮助!