帮助合并两个sql查询(TECDOC)。
如何组合这两个查询?
SELECT
ART_ARTICLE_NR,
SUP_BRAND,
DES_TEXTS.TEX_TEXT AS ART_COMPLETE_DES_TEXT,
DES_TEXTS2.TEX_TEXT AS ART_STATUS_TEXT
FROM ARTICLES
INNER JOIN DESIGNATIONS ON DESIGNATIONS.DES_ID = ART_COMPLETE_DES_ID
INNER JOIN DES_TEXTS ON DES_TEXTS.TEX_ID = DESIGNATIONS.DES_TEX_ID
INNER JOIN SUPPLIERS ON SUP_ID = ART_SUP_ID
INNER JOIN ART_COUNTRY_SPECIFICS ON ACS_ART_ID = ART_ID
INNER JOIN DESIGNATIONS
AS DESIGNATIONS2 ON DESIGNATIONS2.DES_ID = ACS_KV_STATUS_DES_ID
INNER JOIN DES_TEXTS
AS DES_TEXTS2 ON DES_TEXTS2.TEX_ID = DESIGNATIONS2.DES_TEX_ID
WHERE
ART_ID IN (
52277
) AND
DESIGNATIONS.DES_LNG_ID = 19 AND
DESIGNATIONS2.DES_LNG_ID = 19;
SELECT
DES_TEXTS.TEX_TEXT AS CRITERIA_DES_TEXT,
IFNULL(DES_TEXTS2.TEX_TEXT, ACR_VALUE) AS CRITERIA_VALUE_TEXT
FROM
ARTICLE_CRITERIA
LEFT JOIN DESIGNATIONS
AS DESIGNATIONS2 ON DESIGNATIONS2.DES_ID = ACR_KV_DES_ID
LEFT JOIN DES_TEXTS
AS DES_TEXTS2 ON DES_TEXTS2.TEX_ID = DESIGNATIONS2.DES_TEX_ID
LEFT JOIN CRITERIA ON CRI_ID = ACR_CRI_ID
LEFT JOIN DESIGNATIONS ON DESIGNATIONS.DES_ID = CRI_DES_ID
LEFT JOIN DES_TEXTS ON DES_TEXTS.TEX_ID = DESIGNATIONS.DES_TEX_ID
WHERE
ACR_ART_ID IN (
52277
) AND
(DESIGNATIONS.DES_LNG_ID IS NULL OR DESIGNATIONS.DES_LNG_ID = 19) AND
(DESIGNATIONS2.DES_LNG_ID IS NULL OR DESIGNATIONS2.DES_LNG_ID = 19);
CREATE TABLE IF NOT EXISTS articles ( ART_ID int(11) NOT NULL, ART_ARTICLE_NR varchar(66) NOT NULL, ART_SUP_ID smallint(6) DEFAULT NULL, ART_DES_ID int(11) DEFAULT NULL, ART_COMPLETE_DES_ID int(11) DEFAULT NULL, ART_PACK_SELFSERVICE smallint(6) DEFAULT NULL, ART_MATERIAL_MARK smallint(6) DEFAULT NULL, ART_REPLACEMENT smallint(6) DEFAULT NULL, ART_ACCESSORY smallint(6) DEFAULT NULL, ART_BATCH_SIZE1 int(11) DEFAULT NULL, ART_BATCH_SIZE2 int(11) DEFAULT NULL, PRIMARY KEY (ART_ID), KEY ART_ARTICLE_NR (ART_ARTICLE_NR(10)) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS article_criteria ( ACR_ART_ID int(11) NOT NULL, ACR_GA_ID int(11) NOT NULL, ACR_SORT smallint(6) NOT NULL, ACR_CRI_ID smallint(6) NOT NULL, ACR_VALUE varchar(60) DEFAULT NULL, ACR_KV_DES_ID int(11) DEFAULT NULL, ACR_DISPLAY smallint(6) DEFAULT NULL, PRIMARY KEY (ACR_ART_ID,ACR_GA_ID,ACR_SORT) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS art_country_specifics ( ACS_ART_ID int(11) NOT NULL, ACS_PACK_UNIT int(11) DEFAULT NULL, ACS_QUANTITY_PER_UNIT int(11) DEFAULT NULL, ACS_KV_STATUS_DES_ID int(11) DEFAULT NULL, ACS_KV_STATUS varchar(9) DEFAULT NULL, ACS_STATUS_DATE datetime DEFAULT NULL, KEY ACS_ART_ID (ACS_ART_ID) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS criteria ( CRI_ID smallint(6) NOT NULL, CRI_DES_ID int(11) NOT NULL, CRI_SHORT_DES_ID int(11) DEFAULT NULL, CRI_UNIT_DES_ID int(11) DEFAULT NULL, CRI_TYPE binary(1) NOT NULL, CRI_KT_ID smallint(6) DEFAULT NULL, CRI_IS_INTERVAL smallint(6) DEFAULT NULL, CRI_SUCCESSOR smallint(6) DEFAULT NULL, PRIMARY KEY (CRI_ID) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS designations ( DES_ID int(11) NOT NULL, DES_LNG_ID smallint(6) NOT NULL, DES_TEX_ID int(11) NOT NULL, PRIMARY KEY (DES_ID,DES_LNG_ID) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS des_texts ( TEX_ID int(11) NOT NULL, TEX_TEXT text, PRIMARY KEY (TEX_ID) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS suppliers ( SUP_ID smallint(6) NOT NULL, SUP_BRAND varchar(60) DEFAULT NULL, SUP_SUPPLIER_NR smallint(6) DEFAULT NULL, SUP_COU_ID smallint(6) DEFAULT NULL, SUP_IS_HESS smallint(6) DEFAULT NULL, PRIMARY KEY (SUP_ID) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
答案 0 :(得分:0)
我认为这可能有效。请注意,这是不测试(特别是因为你忽略了为我们提供表格的布局),而且我必须猜测articles
是怎么回事与article_criteria
相关联。
SELECT Articles.art_article_nr, Suppliers.sup_brand
Article_Designations.text, Country_Specific_Designations.text,
Article_Criteria_Designations.value,
Article_Criteria_Designations.text
FROM Articles
JOIN (SELECT Designations.des_id, Des_Texts.tex_text as text
FROM Designations
JOIN Des_Texts
ON Des_Texts.tex_id = Designations.des_tex_id
AND Designations.des_lng_id = 19) as Article_Designations
ON Article_Designations.des_id = Articles.art_complete_des_id
JOIN Suppliers
ON Suppliers.sup_id = Articles.art_sup_id
JOIN Art_Country_Specific
ON Art_Country_Specific.acs_art_id = Articles.art_id
JOIN (SELECT Designations.des_id, Des_Texts.tex_text as text
FROM Designations
JOIN Des_Texts
ON Des_Texts.tex_id = Designations.des_tex_id
AND Designations.des_lng_id = 19) as Country_Specific_Designations
ON Country_Specific_Designations.des_id = Art_Country_Specific.acs_kv_status_des_id
LEFT JOIN (SELECT Article_Criteria.acr_art_id,
COALESCE(Art_Cri_Designations.text, Article_Criteria.acr_value) as value,
Criteria_Designations.text
FROM Article_Criteria
LEFT JOIN (SELECT Designations.des_id, Des_Texts.tex_text as text
FROM Designations
JOIN Des_Texts
ON Des_Texts.tex_id = Designations.des_tex_id
AND Designations.des_lng_id = 19) as Art_Cri_Designations
ON Art_Cri_Designations.des_id = Article_Criteria.acr_kv_des_id
LEFT JOIN (SELECT Criteria.cri_id, Des_Texts.tex_text as text
FROM Criteria
JOIN Designations
ON Designations.des_id = Criteria.cri_des_id
JOIN Des_Texts
ON Des_Texts.tex_id = Designations.des_tex_id
AND Designations.des_lng_id = 19) as Criteria_Designations
ON Criteria_Designations.cri_id = Article_Criteria.acr_cri_id
) as Article_Criteria_Designations
ON Article_Criteria_Designations.acr_art_id = Articles.art_id
WHERE Articles.art_id IN (52277)