2012-09-29 76 views
0

大家好,我有一個表業務,有很多關係很多。有人建議我先執行組concat以從多個表中獲取想法,然後查看這些ID以從多個表中獲取值Mysql組concat,然後根據結果查詢

在下面的實例中,我可以看到我獲取了公告ID列表通過GROUP_CONCAT(DISTINCT ba.announcement_id)的 '公告',是如何從這裏做我設置

SELECT * FROM公告 放回原處身份證件(_ __ _ __ _

,其中在代表着什麼從GROUP_CONCAT返回

ID B

BEGIN

/* 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 

回答

0

在你的第一個SELECT語句,你可以指定announcementId的一個變量,然後用它來獲得所有公告在第二個查詢:

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

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

一些鏈接: