gpt4 book ai didi

sql - 选择未分配的硬件

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

我有这个数据:

enter image description here

enter image description here

如何选择尚未分配的硬件?

有两种情况:

  1. employee_hardware

    中不存在的硬件
  2. employee_hardware 中存在但没有的硬件allocated = TRUE

这是我的尝试,但它不正确,因为它显示了多行相同的硬件。

SELECT h.hardware_id
,h.hardware_name
,h.created_at
,h.updated_at
FROM hardware h
LEFT JOIN employee_hardware eh ON h.hardware_id = eh.hardware_id
WHERE h.hardware_type_id = 2
AND (
eh.employee_id IS NULL
OR eh.allocated = FALSE
)
ORDER BY h.hardware_id

enter image description here

最佳答案

你可以试试这个

SELECT h.*
FROM hardware h
WHERE NOT EXISTS ( SELECT 'a'
FROM employee_hardware eh
WHERE h.hardware_id = eh.hardware_id
AND eh.allocated = TRUE
)

关于sql - 选择未分配的硬件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29844187/

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