gpt4 book ai didi

MySQL if 条件则左连接

转载 作者:行者123 更新时间:2023-11-30 23:03:42 24 4
gpt4 key购买 nike

我在页面底部有一个有效的选择。如您所见,它有 53 行,在我看来,这太多了。有人告诉我,在 MySQL 中有一个可用的“if”条件,但我无法让它工作。工作 Select unions 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

最佳答案

您可以只执行一个查询,其中包含每个左联接,然后使用 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

关于MySQL if 条件则左连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22779866/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com