2014-07-08 77 views
0

我必須在3個不同的表中搜索一個術語。 這是我的3個要求,我只想做一個。 表是teledis,afr,software_cache。 如果你想,這裏是我的架構:http://imgur.com/aHqtE5P單個請求使用連接表在多個表中搜索

SELECT teledis.nameid 
FROM teledis 
LIKE vulcain = '%XXX%'; 



SELECT teledis.nameid 
FROM joinafr 
    JOIN teledis 
     ON joinafr.teledis_id = teledis.idteledis 
    JOIN afr 
     ON joinafr.afr_id = afr.idafr 
WHERE afr.name LIKE '%XXX%'; 



SELECT teledis.nameid 
FROM softs 
    JOIN software_cache 
     ON softs.id_soft_cache = software_cache.id 
    JOIN joinsoft 
     ON softs.idsofts = joinsoft.soft_id 
    JOIN teledis 
     ON joinsoft.soft_id = teledis.idteledis WHERE software_cache.name LIKE '%XXX'; 

回答

1
SELECT teledis.nameid 
FROM teledis 
WHERE vulcain LIKE '%XXX%' 
UNION 
SELECT teledis.nameid 
FROM joinafr 
    JOIN teledis 
     ON joinafr.teledis_id = teledis.idteledis 
    JOIN afr 
     ON joinafr.afr_id = afr.idafr 
WHERE afr.name LIKE '%XXX%' 
UNION 
SELECT teledis.nameid 
FROM softs 
    JOIN software_cache 
     ON softs.id_soft_cache = software_cache.id 
    JOIN joinsoft 
     ON softs.idsofts = joinsoft.soft_id 
    JOIN teledis 
     ON joinsoft.soft_id = teledis.idteledis WHERE software_cache.name LIKE '%XXX'; 
1

你可以嘗試這樣的事情:

SELECT teledis.nameid, afr.name, software_cache.name 
FROM softs 
JOIN software_cache 
    ON softs.id_soft_cache = software_cache.id 
JOIN joinsoft 
    ON softs.idsofts = joinsoft.soft_id 
JOIN joinafr 
    ON joinafr.teledis_id = teledis.idteledis 
JOIN teledis 
    ON joinsoft.soft_id = teledis.idteledis 
JOIN afr 
    ON joinafr.afr_id = afr.idafr 
WHERE afr.name LIKE '%XXX%' OR software_cache.name LIKE '%XXX';