2016-06-22 28 views
0

異なる結果セットを持つ2つのSQLクエリがあります。彼らは両方とも同じテーブルから来ましたが、2番目のクエリは別のテーブルに依存します。どのようにUNIONを使用せずにこれらの2つのクエリを組み合わせることができますか?MySQL - UNIONを使わずに2つのクエリを組み合わせることは可能ですか?

これは私の質問です。

SELECT tmp.id, tmp.title , tmp.description, asst.smallUrl, b1.paramVal as duration, b2.paramVal as clips, asst.fileUrl as video 
FROM listDb.baseData tmp 
INNER JOIN listDb.tag tag ON tag.baseDataId= tmp.id and tag.tag = 'service app' and tag.status = "active" 
INNER JOIN listDb.baseParam b0 ON b0.baseDataId= tmp.id 
and ((b0.paramName = "role" 
and (b0.paramVal = "public")) 
or ((select count(*) from listDb.baseParam temp 
where temp.baseDataId= tmp.id and paramName = "role")=0)) 
or (b0.paramName = "role" and b0.paramVal = "public" and tmp.owner = 27) 
LEFT JOIN listDb.baseParam b1 ON b1.baseDataId= tmp.id and b1.paramName="duration" and b1.status = "active" 
LEFT JOIN listDb.baseParam b2 ON b2.baseDataId= tmp.id and b2.paramName=" itemCount" and b2.status = "active" 
LEFT JOIN listDb.baseParam b3 ON b3.baseDataId= tmp.id and b3.paramName="previewUrl" and b3.status = "active" 
LEFT JOIN assetDb.baseData asst ON asst.id = b3.paramVal and asst.status = "active" 
WHERE tmp.status = "active" and tmp.application = "template" and tmp.role = "public" 

UNION 

SELECT tmp.id, tmp.title , tmp.description, asst.smallUrl, b1.paramVal as duration, b2.paramVal as clips, asst.fileUrl as video 
FROM listDb.baseData tmp 
INNER JOIN listDb.tag tag ON tag.baseDataId= tmp.id and tag.tag = 'service app' and tag.status = "active" 
INNER JOIN listDb.baseParam b0 ON b0.baseDataId= tmp.id 
and ((b0.paramName = "role" 
and (b0.paramVal = "private" or b0.paramVal = "" and b0.paramVal != "public")) 
or ((select count(*) from listDb.baseParam temp 
where temp.baseDataId= tmp.id and paramName = "role")=0)) 
or (b0.paramName = "role" and b0.paramVal = "public" and tmp.owner = 27) 
LEFT JOIN listDb.baseParam b1 ON b1.baseDataId= tmp.id and b1.paramName="duration" and b1.status = "active" 
LEFT JOIN listDb.baseParam b2 ON b2.baseDataId= tmp.id and b2.paramName="itemCount" and b2.status = "active" 
LEFT JOIN listDb.baseParam b3 ON b3.baseDataId= tmp.id and b3.paramName="previewUrl" and b3.status = "active" 
LEFT JOIN assetDb.baseData asst ON asst.id = b3.paramVal and asst.status = "active" 
INNER JOIN listDb.checkRestricted cr ON cr.baseDataId= tmp.id and cr.status = "active" and cr.owner = 27 
WHERE tmp.status = "active" and tmp.application = "template" and tmp.role = "private" 
+0

は "この'(b0.paramVal = "プライベート" またはb0.paramVal =除くクエリの間に違い" がありますとb0.paramVal!= "public") '? –

+0

2番目のクエリは、checkRestrictedテーブルの別のテーブルに基づいて結果を取得します.. INNER JOIN listDb.checkRestricted cr ON cr.baseDataId = tmp.idおよびcr.status = "active"およびcr.owner = 27 –

答えて

0

こんにちはあなたはそう組合

SELECT tmp.id, tmp.title, tmp.description, asst.smallUrl, b1.paramVal AS duration, b2.paramVal AS clips, asst.fileUrl AS video 
FROM listDb.baseData tmp INNER JOIN listDb.tag tag ON tag.baseDataId= tmp.id AND tag.tag = 'service app' AND tag.status = "active" 
INNER JOIN listDb.baseParam b0 ON b0.baseDataId= tmp.id AND ((b0.paramName = "role" AND ((b0.paramVal = "public") OR (b0.paramVal = "private" OR b0.paramVal = ""))) OR ((
SELECT COUNT(*) FROM listDb.baseParam temp WHERE temp.baseDataId= tmp.id AND paramName = "role")=0)) OR (b0.paramName = "role" AND b0.paramVal = "public" AND tmp.owner = 27) 
LEFT JOIN listDb.baseParam b1 ON b1.baseDataId= tmp.id AND b1.paramName="duration" AND b1.status = "active" 
LEFT JOIN listDb.baseParam b2 ON b2.baseDataId= tmp.id AND b2.paramName=" itemCount" AND b2.status = "active" 
LEFT JOIN listDb.baseParam b3 ON b3.baseDataId= tmp.id AND b3.paramName="previewUrl" AND b3.status = "active" 
LEFT JOIN assetDb.baseData asst ON asst.id = b3.paramVal AND asst.status = "active" 
WHERE tmp.status = "active" AND tmp.application = "template" AND (tmp.role = "public" OR tmp.role = "private") group by tmp.id 
+0

こんにちは。私はあなたに問い合わせました。これは正しい結果を返しません。 –

0

を使用せずに、これらのクエリを試すことができ、クエリの違いは次のとおりです。

  • 最初のものは、第1 b0.paramVal = "public"公共 "データ"
  • を返します。 - b0.paramVal = "private" or b0.paramVal = ""プライベートまたはプライベート制限付きチェック(checkRestrictedテーブル)

と論理的にそれがある:

(b0.paramVal = "public") 
OR (b0.paramVal in ("private", "") AND EXISTS(...listDb.checkRestricted) 

完全なソース:

SELECT 
    tmp.id, tmp.title , tmp.description, asst.smallUrl, 
    b1.paramVal as duration, b2.paramVal as clips, asst.fileUrl as video 
FROM listDb.baseData tmp 
INNER JOIN listDb.tag tag 
    ON tag.baseDataId= tmp.id and tag.tag = 'service app' and tag.status = "active" 
INNER JOIN listDb.baseParam b0 
    ON b0.baseDataId= tmp.id 
    AND 
    (
    b0.paramName = "role" 
    OR 
    (
     NOT EXISTS(select 1 from listDb.baseParam temp 
     where temp.baseDataId= tmp.id and paramName = "role")     ---<<< does this actually mean LEFT JOIN to listDb.baseParam b0 ??? 
    ) 
) 
    or (b0.paramName = "role" and b0.paramVal = "public" and tmp.owner = 27) ---<<< this is very strange in join 
LEFT JOIN listDb.baseParam b1 
    ON b1.baseDataId= tmp.id and b1.paramName="duration" and b1.status = "active" 
LEFT JOIN listDb.baseParam b2 
    ON b2.baseDataId= tmp.id and b2.paramName=" itemCount" and b2.status = "active" 
LEFT JOIN listDb.baseParam b3 
    ON b3.baseDataId= tmp.id and b3.paramName="previewUrl" and b3.status = "active" 
LEFT JOIN assetDb.baseData asst 
    ON asst.id = b3.paramVal and asst.status = "active" 
WHERE tmp.status = "active" 
    and tmp.application = "template" 
    /* changes to filter (some alterations might be performed if there must be LEFT JOIN to baseparam0 */ 
    and tmp.role in ("public", "private") 
    and (b0.paramVal = "public" 
    OR b0.paramVal in ("private", "") 
     AND EXISTS(SELECT 1 FROM listDb.checkRestricted cr 
     WHERE cr.baseDataId= tmp.id and cr.status = "active" and cr.owner = 27) 
) 
+0

あなたが与えたクエリに基づいて、私は正しい結果を得ることができません..それは私に構文エラーを与える。 –

+0

論理エラーを最初に修正することをお勧めします。不合理な出力を生成するかもしれない結合の奇妙な条件のように。 –

関連する問題