MYSQL帮助 - 2个数据库,1个特色图片

时间:2016-03-04 01:04:53

标签: mysql select case

我目前有2个MYSQL数据库。

DatabaseOne是包含所有汽车的完整数据库,DatabaseTwo专注于汽车制造商的具体细节。例如:

DatabaseOne拥有汽车制造商,型号,年份,颜色以及链接到汽车图片

DatabaseTwo有制造商,制造商的详细信息,电话号码,网站等,以及 featured_image_id ,我可以设置它来突出显示来自DatabaseOne的特定汽车,加入匹配的ID。

表数据DatabaseOne:

    ID   |   Manufacturer   |   Model   |   Year   |   Color   |   ImageLink
    1    |   Toyota         |   Camry   |   2002   |   Black   |   88.jpg
    2    |   Toyota         |   Venza   |   2010   |   Black   |   91.jpg
    3    |   Mazda          |   Miata   |   2001   |   Red     |   77.jpg
    4    |   Mercedes       |   Benz    |   2012   |   Green   |   46.jpg
    5    |   Mercedes       |   Benz    |   2000   |   Blue    |   12.jpg

表数据DatabaseTwo:

    ID   |   Manufacturer   |   PhNumber   |   Website   |   FeaturedImage
    1    |   Toyota         |   555-5555   |   .com      |        2
    2    |   Mazda          |   555-5555   |   .com      |        3
    3    |   Mercedes       |   555-5555   |   .com      |        

当DatabaseTwo的FeaturedImage与DatabaseOne的ID匹配时,数据库会加入。

在这种情况下,丰田将展示DatabaseOne ID 2(Venza)的ImageLink(.jpg),马自达将显示带有DatabaseOne ID 3的马自达图像,并且由于未设置梅赛德斯精选图像,我希望它显示最新的梅赛德斯,这将是DatabaseOne ID 5。

我要做的是显示结果,由DatabaseTwo中的制造商分类,如果在DatabaseTwo中设置了featured_image_ID,我想在DatabaseOne中显示匹配ID的image_link,如果未设置featured_image_id,我只想显示该制造商提供的最新款汽车的image_link。

在显示结果时,如果以下语句中的第二个 CASE 前面带有“或”,则所有车辆的最近图片都会显示,然而< / em>使用featured_image_id设置的制造商也会显示最近关联的image_link。

如果第二个 CASE 前面有“AND”,,则会显示具有featured_image_id设置的制造商,并且不会显示没有feature_image_id的其他制造商。

我正在尝试确保所有制造商都显示图像,无论是专门设置在DatabaseTwo.featured_image_id中,还是未设置,然后由匹配的制造商回退到最新的图像ID。

这是我目前的MYSQL代码:

    SELECT DatabaseOne.id, DatabaseOne.manufacturer, 
    DatabaseOne.model,DatabaseOne.year, DatabaseOne.picture, 
    DatabaseOne.image_link, DatabaseTwo.featured_image_id, 
    DatabaseTwo.title, DatabaseTwo.id, 
    IF(DatabaseTwo.id != 0, DatabaseTwo.id, 'N/A') AS card_id 
    FROM DatabaseOne 
    LEFT JOIN DatabaseTwo
    ON (CASE 
    WHEN (DatabaseTwo.featured_image_id != 0 AND DatabaseTwo.category = 'Manufacturer' 
    AND DatabaseTwo.Manufacturer = DatabaseOne.Manufacturer) 
    THEN (DatabaseTwo.featured_image_id = DatabaseOne.id) 
    END ) 
    WHERE DatabaseOne.Manufacturer != ''  
    AND DatabaseOne.id != 0 
    OR ((CASE 
    WHEN (DatabaseTwo.featured_image_id IS NOT NULL) 
    THEN (DatabaseTwo.featured_image_id != 0 ) END) ) 
    GROUP BY DatabaseOne.Manufacturer
    ORDER BY DatabaseOne.Manufacturer ASC, DatabaseOne.id DESC 

1 个答案:

答案 0 :(得分:1)

请尝试以下查询:

SELECT t1.ID, t1.Manufacturer, t1.Model, t1.Year, t1.Color,
    t3.ImageLink
FROM DatabaseOne t1 LEFT JOIN
(
    SELECT db2.Manufacturer,
        COALESCE(db2.FeaturedImage, t.DefaultFeaturedImage) AS FeaturedImage
    FROM DatabaseTwo db2
    INNER JOIN
    (
        SELECT MAX(ID) AS DefaultFeaturedImage, Manufacturer
        FROM DatabaseOne
        GROUP BY Manufacturer
    ) t
        ON db2.Manufacturer = t.Manufacturer
) t2
    ON t1.Manufacturer = t2.Manufacturer
INNER JOIN DatabaseOne t3
    ON t2.FeaturedImage = t3.ID

请按照以下链接查看正在运行的演示:

SQLFiddle