gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:48:43 25 4
gpt4 key购买 nike

我有一个多态表 creationables 并且无法根据 creationable_type 找出返回所有 creationables 的条件查询的关键部分> 字段值(humanrobot),基于它们是否已从各自的表中删除(humansrobots,以及 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com