MySQL选择ID在哪里(选择另一个表)

时间:2018-09-20 07:24:18

标签: php mysql sql select

问题是我需要从表中选择值

category_filter_relations

id   modification_id

我输入的modification_id等于变量,并且我还需要联接另一个表

category_filter_relations_items

id   modification_id   category_filter_relation_id

其中可以有多个条目,它们具有相同的modification_id和不同的category_filter_relation_id,我需要选择那些category_filter_relations项。

我的查询:

SELECT cfr.id AS category_filter_relation_id, 
       cfr.modification_id,
FROM category_filter_relations cfr
WHERE 1
AND cfr.modification_id = '18340300'
AND category_filter_relation_id 
IN (SELECT category_filter_relations_items.category_filter_relation_id 
    FROM category_filter_relations_items 
    WHERE category_filter_relations_items.modification_id = '18340300')

这样我得到一个错误:

  

“ IN / ALL / ANY子查询”中的未知列“ category_filter_relation_id”

我也试图加入

LEFT JOIN category_filter_relations_items ON (category_filter_relations_items.category_filter_relation_id = category_filter_relation_id)

但是通过这种方式,我从category_filter_relations_items获取了所有条目  表,我需要

中的条目
category_filter_relations where category_filter_relations.modification_id = 18340300

加入

category_filter_relations_items.category_filter_relation_id where category_filter_relations_items.modification_id = 18340300

我需要的数据示例:

category_filter_relations
id    modification_id
23796 18340300

category_filter_relations_items 
id      category_filter_relation_id  modification_id     
54690   23270                        18340300

我正在选择表category_filter_relations,结果应为:

id    modification_id
23796 18340300
23270 11111111

ID为23270,因为category_filter_relations_items.modification_id等于18340300

1 个答案:

答案 0 :(得分:1)

这是您的表格:

category_filter_relations
=========================
id |  modification_id

category_filter_relations_items
===================================================
id | modification_id | category_filter_relation_id

在查询中,您执行以下操作:

SELECT cfr.id AS category_filter_relation_id, -- You select the category_filter_relations id
       cfr.modification_id,                   -- You select the modification_id
FROM category_filter_relations cfr            -- Both from the table category_filter_relations
WHERE 1
AND cfr.modification_id = '18340300'          -- You add a condition on the modification_id
AND category_filter_relation_id               -- You add a condition on an id not in the table category_filter_relations
IN (SELECT category_filter_relations_items.category_filter_relation_id 
    FROM category_filter_relations_items 
    WHERE category_filter_relations_items.modification_id = '18340300')

您想要的输出是:

id    modification_id
23796 18340300
23270 11111111

如果添加条件AND cfr.modification_id = '18340300',这怎么可能? 如果只要求modification_id = 11111111,就不会得到modification_id = '18340300'

所以也许尝试:

SELECT cfr.id, 
       cfr.modification_id,
FROM category_filter_relations cfr
WHERE cfr.modification_id = '18340300'

UNION

SELECT cfri.id, 
       cfri.modification_id,
FROM category_filter_relations_items cfri
INNER JOIN category_filter_relations cfr ON cfr.id = cfri.category_filter_relation_id
    AND cfr.modification_id = '18340300'

这样,您将从category_filter_relations中获得第一行,列名称为idid的{​​{1}})和category_filter_relations({{1} modification_id中的}由于条件而等于'18340300'。

modification_id现在也将具有列category_filter_relations,但是现在是表UNION的{​​{1}}和列id将成为{ id的行中的{1}},其中category_filter_relations_items是表modification_id的{​​{1}},其中具有modification_id

是您要找的东西吗?