MySQL如果条件然后离开加入

时间:2014-04-01 07:55:11

标签: mysql

我在页面底部有一个工作选择。正如你所看到的,它有53行,在我看来,这太过分了。有人告诉我,有一个' if'在MySQL中可用的条件,但我无法使其工作。工作选择联合4选择,因为在每个选择中,我需要加入另一个表。是否可以根据内容加入表格?

SELECT *
FROM (
    SELECT m.`identificator` , ml.id AS link_id, ml.parent, ml.type, 
            ml.destination, ml.disabled, ml.order, mld.`name` AS link_name, 
            mld.`alt` , mld.`title` , cpd.`slug` 
    FROM  `wf_menu` m
    LEFT JOIN  `wf_menu_link` ml ON m.`id` = ml.`menu_id` 
    LEFT JOIN  `wf_menu_link_desc` mld ON ml.`id` = mld.`link_id` 
    LEFT JOIN  `wf_cms_post_desc` cpd ON ml.destination = cpd.post_id
    WHERE mld.`lang_id` =1
    AND mld.`lang_id` = cpd.`lang_id` 
    AND (ml.`type` =  'page'
    OR ml.`type` =  'article')

    UNION

    SELECT m.`identificator` , ml.id AS link_id, ml.parent, ml.type, 
            ml.destination, ml.disabled, ml.order, mld.`name` AS link_name, 
            mld.`alt` , mld.`title` , cpd.`slug` 
    FROM  `wf_menu` m
    LEFT JOIN  `wf_menu_link` ml ON m.`id` = ml.`menu_id` 
    LEFT JOIN  `wf_menu_link_desc` mld ON ml.`id` = mld.`link_id` 
    LEFT JOIN  `wf_cms_category_desc` cpd ON ml.destination = cpd.category_id
    WHERE mld.`lang_id` =1
    AND mld.`lang_id` = cpd.`lang_id` 
    AND ml.`type` =  'cmscat'

    UNION

    SELECT m.`identificator` , ml.id AS link_id, ml.parent, ml.type, 
            ml.destination, ml.disabled, ml.order, mld.`name` AS link_name, 
            mld.`alt` , mld.`title` , spd.`slug` 
    FROM  `wf_menu` m
    LEFT JOIN  `wf_menu_link` ml ON m.`id` = ml.`menu_id` 
    LEFT JOIN  `wf_menu_link_desc` mld ON ml.`id` = mld.`link_id` 
    LEFT JOIN  `wf_shop_category_desc` spd ON ml.destination = spd.category_id
    WHERE mld.`lang_id` =1
    AND mld.`lang_id` = spd.`lang_id` 
    AND ml.`type` =  'shopcat'

    UNION

    SELECT m.`identificator` , ml.id AS link_id, ml.parent, ml.type, 
            ml.destination, ml.disabled, ml.order, mld.`name` AS link_name, 
            mld.`alt` , mld.`title` , 'link' as slug 
    FROM  `wf_menu` m
    LEFT JOIN  `wf_menu_link` ml ON m.`id` = ml.`menu_id` 
    LEFT JOIN  `wf_menu_link_desc` mld ON ml.`id` = mld.`link_id` 
    WHERE mld.`lang_id` =1
    AND ml.`type` =  'link'

) a
ORDER BY `order` DESC

2 个答案:

答案 0 :(得分:0)

我认为您需要使用动态SQL。我的意思是,将结束SQL构建为临时变量并执行它:

set @query = (SELECT ...);
PREPARE st FROM @query;
EXECUTE st;

你的第一个案例可能是:

SET @query= (SELECT CONCAT(GROUP_CONCAT(CONCAT('SELECT m.`identificator` , ml.id AS link_id, ml.parent, ml.type, 
            ml.destination, ml.disabled, ml.order, mld.`name` AS link_name, 
            mld.`alt` , mld.`title` , cpd.`slug` FROM  `wf_menu` m LEFT JOIN  `wf_menu_link` ml ON m.`id` = ml.`menu_id` ',
            CASE WHEN TTYPES.type='article' THEN 
                CONCAT('LEFT JOIN  `wf_menu_link_desc` mld ON ml.`id` = mld.`link_id` LEFT JOIN  `wf_cms_post_desc` cpd ON ml.destination = cpd.post_id WHERE mld.`lang_id` =1 AND mld.`lang_id` = cpd.`lang_id` AND (ml.`type` =  \'page\' OR ml.`type` =  \'',TTYPES.type,'\')')

            END )
SEPARATOR ' UNION '), ' SELECT NULL FROM wf_menu_link WHERE 1=2') FROM (SELECT DISTINCT wf_menu_link.`type` FROM wf_menu_link) TTYPES);

(您可以完成剩余案例)

运行该查询将使用生成的查询填充@query变量。生成的查询应与您在问题中发布的查询相同。然后你可以在这个答案中看到上面的内容。

这是一个示例/想法,您必须完成它和/或检查它是否适用于您的模型。

答案 1 :(得分:0)

您可以只使用每个左连接执行单个查询,并使用IF语句对生成的“slug”列进行排序。但是,这取决于您想要输出的内容,以及其他表上是否有多个匹配的行。

这样的事情: -

SELECT m.identificator , ml.id AS link_id, ml.parent, ml.type, 
        ml.destination, ml.disabled, ml.order, mld.name AS link_name, 
        mld.alt , mld.title , cpd.slug 
FROM  wf_menu m
LEFT JOIN  wf_menu_link ml ON m.id = ml.menu_id 
LEFT JOIN  wf_menu_link_desc mld ON ml.id = mld.link_id 
LEFT JOIN  wf_cms_post_desc cpd ON ml.destination = cpd.post_id AND (ml.type = 'page' OR ml.type = 'article')
LEFT JOIN  wf_cms_category_desc cpd ON ml.destination = cpd.category_id AND ml.type =  'cmscat' AND mld.lang_id = cpd.lang_id 
LEFT JOIN  wf_shop_category_desc spd ON ml.destination = spd.category_id AND ml.type =  'shopcat' AND mld.lang_id = spd.lang_id 
WHERE mld.lang_id =1
AND mld.lang_id = cpd.lang_id