gpt4 book ai didi

mysql - 如何构建一个 MySql 查询,该查询返回包含数组中所有值的结果?

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

我的 MySql 查询看起来像

SELECT * FROM node as nd
LEFT JOIN term_node as trm_nd ON trm_nd.nid = nd.nid
WHERE nd.title = 'Gadget - definition'
AND nd.type = 'help_system'
AND trm_nd.tid IN (434, 456, 25, 293)

返回结果如

-------------------------------------------------------
nid title type tid
-------------------------------------------------------
26986 Gadget - definition help_system 25
26986 Gadget - definition help_system 293
52421 Gadget - definition help_system 25
52421 Gadget - definition help_system 293
73061 Gadget - definition help_system 25
73061 Gadget - definition help_system 293
86071 Gadget - definition help_system 25
86071 Gadget - definition help_system 293
98596 Gadget - definition help_system 25
98596 Gadget - definition help_system 293
98596 Gadget - definition help_system 434
98596 Gadget - definition help_system 456

但是我需要类似于下面的结果,它需要返回具有与 tid 列中的所有数组值(434, 456, 25, 293)关联的 nid 的值

-------------------------------------------------------
nid title type tid
-------------------------------------------------------
98596 Gadget - definition help_system 25
98596 Gadget - definition help_system 293
98596 Gadget - definition help_system 434
98596 Gadget - definition help_system 456

最佳答案

在现有查询中使用 GROUP BY 并将其设为子查询,如下所示:

SELECT * FROM
node n1
JOIN (
SELECT nid FROM node as nd
LEFT JOIN term_node as trm_nd ON trm_nd.nid = nd.nid
WHERE nd.title = 'Gadget - definition'
AND nd.type = 'help_system'
AND trm_nd.tid IN (434, 456, 25, 293)
GROUP BY nid
HAVING (COUNT(DISTINCT tid)) = 4
) t1
ON n1.nid = t1.nid

关于mysql - 如何构建一个 MySql 查询,该查询返回包含数组中所有值的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22525236/

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