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

标签 mysql sql polymorphism

我有一个多态表creationables,但无法找出基于creationable_type返回所有creationables的条件查询的关键部分> 字段值(人类机器人),基于它们是否从各自的表中删除(人类机器人,以及 robotsrobot_upgrades 组合)。以下是伪查询后的 4 个表:

<强>1。可创作

| 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。机器人升级

| 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
不应返回 id 为 3 的

creationables 行,因为即使其机器人未从 robots 中删除,其相关的 robot_upgrade_id 1 也会被删除在robot_upgrades中。

最佳答案

没有直接的方法可以做到这一点。 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);

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

关于MySQL - 基于多态表值的条件查询结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56230943/

相关文章:

sql - 如何在实际项目中使用数据库触发器?

sql - Access 数据库中的双重内部连接

C++多态、函数调​​用

php - Laravel 4,一个模型的多个多态关系

java - 如何从 Java 代码生成 Go 代码?

php - 你能告诉我我博客的这段代码有什么问题吗?

php - Laravel 原始查询 - 运行此查询不会选择 id

sql - 比较外键 - PostgreSQL

mysql - 正确的 pdo 连接以使用重音

php - 将 MySQL 结果集转换为 JSON 数组以使用 Google GCM API 时出现的问题