0

大家好,我有一个表业务,它有很多多对多的关系。建议我先执行一个组 concat 以从 many 表中获取想法,然后查看这些 id 以从 many 表中获取值

在下面的实例中,好的,我可以看到我通过 GROUP_CONCAT(DISTINCT ba.announcement_id) 作为“公告”获得公告 ID 列表,我如何从这里设置

SELECT * from notice where id IN( _ __ _ __ _ )

其中 in 表示从 group_concat 返回的内容

身份证

开始

/* Business Information and Categories */
SELECT 
    b.alias_title, b.title, b.premisis_name, 
    a.address_line_1, a.address_line_2, a.postal_code,tvc.town_village_city,spc.state_province_county, c.country,
    GROUP_CONCAT(DISTINCT be.event_id) as 'event',
    GROUP_CONCAT(DISTINCT ba.announcement_id) as 'announcement', 
    GROUP_CONCAT(DISTINCT bd.document_id) as 'document',
    GROUP_CONCAT(DISTINCT bi.image_id) as 'image',
    GROUP_CONCAT(DISTINCT bprod.product_id ) as 'product',
    GROUP_CONCAT(DISTINCT bt.tag_title_id) as 'tag'
    FROM business AS b
    INNER JOIN business_category bc_1 ON b.primary_category = bc_1.id
    INNER JOIN business_category bc_2 ON b.secondary_category = bc_2.id
    LEFT OUTER JOIN business_category bc_3 ON b.tertiary_category = bc_3.id
    INNER JOIN address a ON b.address_id = a.id
    LEFT OUTER JOIN town_village_city tvc ON a.town_village_city_id = tvc.id
    LEFT OUTER JOIN state_province_county spc ON a.state_province_county_id
    INNER JOIN country c ON a.country_id = c.id
    LEFT OUTER JOIN geolocation g ON b.geolocation_id = g.id
    LEFT OUTER JOIN business_event be ON b.id = be.event_id
    LEFT OUTER JOIN business_announcement ba ON b.id = ba.announcement_id
    LEFT OUTER JOIN business_document bd ON b.id = bd.business_id
    LEFT OUTER JOIN business_image bi ON b.id = bi.business_id
    LEFT JOIN business_property bp ON b.id= bp.business_id
    LEFT JOIN business_product bprod ON b.id= bprod.business_id 
    LEFT JOIN business_tag bt ON b.id = bt.business_id
    WHERE b.id= in_business_id;

    SELECT * from announcement 
    where 




END
4

1 回答 1

0

在您的第一个选择语句中,您可以将announcementId 分配给一个变量,然后使用它来获取第二个查询中的所有公告:

set @announcementIds = '';
select ...........,
@announcementIds:= GROUP_CONCAT(DISTINCT announcement_id) as 'announcement',
...........;

Select * from announcement 
where announcement_id REGEXP REPLACE(@announcementIds,',','|');

一些链接:

于 2012-09-29T16:39:32.430 回答