使用连接表在多个表中单个请求搜索

时间:2014-07-08 13:47:32

标签: mysql sql

我必须在一个术语中搜索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';

2 个答案:

答案 0 :(得分: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 :(得分: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';