大家好我正在使用sql子查询IN进行两次sql查询.am获取所有id的数据但我需要获取perticualar id的数据我怎么能写下面是我的查询
SELECT g.title
FROM`object_reference` b, object_data a,`object_reference` d,
`tree` e,`object_reference` f,`object_data` g,`tree` c
WHERE a.`obj_id` = b.`obj_id`
AND b.`ref_id` = c.`parent`
AND c.`child` = d.`ref_id`
AND d.`ref_id` = e.`parent`
AND e.`child` = f.`ref_id`
AND f.`obj_id` = g.`obj_id`
AND a.type='tst' IN(
SELECT c.child
FROM object_data a,`object_reference` b,`tree` c
WHERE a.`obj_id` = b.`obj_id`
AND b.`ref_id` = c.`parent`
AND a.obj_id=3217)
以下是一些示例数据:
table object_data:
obj_id | type | title
-------+------+------
3217 |crs |it
3221 |grp |xyz
3228 |tst |test
3264 |tst |test3
table object_reference:
ref_id | obj_id
-------+---------
337 |3217
338 |3221
343 |3228
371 |3264
table tree:
tree | child | parent
-----+-------+------
1 |338 |337
2 |343 |338
3 |371 |337
以下是样本数据的预期结果:
obj_id|title
------+-----
3228 |test
3264 |test3
答案 0 :(得分:0)
尝试此查询:
SELECT A.obj_id, A.title
FROM object_data A JOIN object_reference B
ON A.obj_id=B.obj_id
JOIN (SELECT C.child FROM tree C
WHERE NOT EXISTS (SELECT null
FROM. tree D
WHERE D.parent=C.child)) E
ON B.ref_id=E.child;