2016-06-28 6 views
3

私は今、より高度な方法でMySQLクエリを使用する方法を学び始めたばかりなので、これを説明する方法は分かりません。ここで私が持っているものです。複数の属性を持つ列をMySQLに追加する

SELECT pa.displayvalue as Brand 
FROM product p 
JOIN categoryheader ch ON ch.templatetype = 0 AND p.categoryid=ch.categoryid AND ch.isactive=1 
JOIN categorydisplayattributes cda ON ch.headerid = cda.headerid AND ch.categoryid = cda.categoryid AND cda.templatetype = ch.templatetype AND cda.isactive=1 
JOIN productattribute pa ON p.productid = pa.productid AND cda.attributeid = pa.attributeid AND pa.localeid = 1 AND pa.isactive =1 
JOIN attributenames an ON pa.attributeid = an.attributeid AND an.localeid = 1 
WHERE p.isactive = 1 AND p.categoryid = 4871 
AND an.attributeid = 113319; 

だから私は、そのクエリを持っており、文字通り、私がやりたいすべては、私はちょうど追加したい

「= 113319とan.attributeid」のための別の列を追加することです別の列が113319の代わりに、1762という値を隣の列に引っ張りたいのです。

したがって、列1には「And.attributeid = 113319」という値がすべて格納され、列2には「And.attributeid = 1762」という値がすべて格納されます。

私は一種の仕事にこのコードを持って:

SELECT pa.displayvalue as Brand 
FROM product p 
JOIN categoryheader ch ON ch.templatetype = 0 AND p.categoryid=ch.categoryid AND ch.isactive=1 
JOIN categorydisplayattributes cda ON ch.headerid = cda.headerid AND ch.categoryid = cda.categoryid AND cda.templatetype = ch.templatetype AND cda.isactive=1 
JOIN productattribute pa ON p.productid = pa.productid AND cda.attributeid = pa.attributeid AND pa.localeid = 1 AND pa.isactive =1 
JOIN attributenames an ON pa.attributeid = an.attributeid AND an.localeid = 1 
WHERE p.isactive = 1 AND p.categoryid = 4871 
AND an.attributeid = 113319; 

SELECT pa.displayvalue as Type 
FROM product p 
JOIN categoryheader ch ON ch.templatetype = 0 AND p.categoryid=ch.categoryid AND ch.isactive=1 
JOIN categorydisplayattributes cda ON ch.headerid = cda.headerid AND ch.categoryid = cda.categoryid AND cda.templatetype = ch.templatetype AND cda.isactive=1 
JOIN productattribute pa ON p.productid = pa.productid AND cda.attributeid = pa.attributeid AND pa.localeid = 1 AND pa.isactive =1 
JOIN attributenames an ON pa.attributeid = an.attributeid AND an.localeid = 1 
WHERE p.isactive = 1 AND p.categoryid = 4871 
AND an.attributeid = 1762; 

をしかし、これは、二つの別々の結果としてのNavicatで返されます。私は、これらの両方を1つの結果ではなく2つの列として、それぞれ1つの列を持つ2つの結果の代わりにしたいと思います。

ご覧のとおり、すべての行は、最後の行を除いて、それぞれの選択肢で同じです。

また、an.attributeidの別の列を取得するために、そのコード全体を2回使用するよりも良い方法があるように感じます。それにもかかわらず、私は私が得ることを取る。

ご意見がありましたら、どうもありがとうございます。

+1

あなたがしていることはやりにくく、99.999999999%の時間は良い考えではありません。行は関連するデータを表すものであり、関連しない列を描画したページの行ではありません。そのような出力を探しているときは、アプリケーションレイヤーで処理することをお勧めします。 – Uueerdo

+0

@Uueerdoは完全に同意します。アプリケーションレベルで2つの処理方法を追加します:2つのクエリを実行して結果を並べて表示するか、1つの中間クエリを実行して、両方の結果を含むテーブルを1つのテーブルに作成します。そのテーブルに対してクエリを実行します。 – qoba

+0

私はこれらの数字を変更しようとしているので、製品の属性を引き出しています。その結果、1つの数字が一連の商品のブランド名を引き出し、それ以外の数字がすべてのブランドタイプを引き出します。私がしようとしていることは、ブランドとタイプをリストアップするCSVを作ることです。どちらもお互いに関連しています。したがって、これが優れていれば、ヘッダーは「ブランド」と「タイプ」となり、各行には各製品のブランドとタイプが入力されます。 – DarkMatter

答えて

1

もしあなたが本当にそれをやりたいのであれば、フォーマットや報告の目的のためです。これはあなたが使用するハックです。

LEFT JOINを使用すると、すべての行が左側の表から返され、右側の表に一致する行が戻されます。一致するものがなければ右側にNULLの結果が出ます。

SELECT p.productid, t2.Type, t1.Brand, t3.Resolution 
FROM product p 
JOIN 
( 
    SELECT p.productid, pa.displayvalue as Brand 
    FROM product p 
    JOIN categoryheader ch ON ch.templatetype = 0 AND p.categoryid=ch.categoryid AND ch.isactive=1 
    JOIN categorydisplayattributes cda ON ch.headerid = cda.headerid AND ch.categoryid = cda.categoryid AND cda.templatetype = ch.templatetype AND cda.isactive=1 
    JOIN productattribute pa ON p.productid = pa.productid AND cda.attributeid = pa.attributeid AND pa.localeid = 1 AND pa.isactive =1 
    JOIN attributenames an ON pa.attributeid = an.attributeid AND an.localeid = 1 
    WHERE p.isactive = 1 AND p.categoryid = 4871 
    AND an.attributeid = 113319 
) t1 ON t1.productid = p.productid 
LEFT OUTER JOIN 
( 
    SELECT p.productid, pa.displayvalue as Type 
    FROM product p 
    JOIN categoryheader ch ON ch.templatetype = 0 AND p.categoryid=ch.categoryid AND ch.isactive=1 
    JOIN categorydisplayattributes cda ON ch.headerid = cda.headerid AND ch.categoryid = cda.categoryid AND cda.templatetype = ch.templatetype AND cda.isactive=1 
    JOIN productattribute pa ON p.productid = pa.productid AND cda.attributeid = pa.attributeid AND pa.localeid = 1 AND pa.isactive =1 
    JOIN attributenames an ON pa.attributeid = an.attributeid AND an.localeid = 1 
    WHERE p.isactive = 1 AND p.categoryid = 4871 
    AND an.attributeid = 1100 
) t2 ON t2.productid = p.productid 
LEFT OUTER JOIN 
( 
    SELECT p.productid, pa.displayvalue as Resolution 
    FROM product p 
    JOIN categoryheader ch ON ch.templatetype = 0 AND p.categoryid=ch.categoryid AND ch.isactive=1 
    JOIN categorydisplayattributes cda ON ch.headerid = cda.headerid AND ch.categoryid = cda.categoryid AND cda.templatetype = ch.templatetype AND cda.isactive=1 
    JOIN productattribute pa ON p.productid = pa.productid AND cda.attributeid = pa.attributeid AND pa.localeid = 1 AND pa.isactive =1 
    JOIN attributenames an ON pa.attributeid = an.attributeid AND an.localeid = 1 
    WHERE p.isactive = 1 AND p.categoryid = 4871 
    AND an.attributeid = 1762 
) t3 ON t3.productid = p.productid 
+0

これは、1mil +行のデータを作成することになりますが、それは27だけにする必要があります。 – DarkMatter

+0

ここでは、単一行の値を出力するだけの場合は、ハードコードされたキー '1'を使用しました。複数の行がある場合は、 'JOIN'を外部キーとリンクする方がよいでしょう。私はあなたが使用できる製品IDがあると思います。 – ydoow

+0

@DarkMatterサンプルデータと必要な出力を提供できる方が良いでしょう。 – ydoow

1

あなたは、これはあなたに1つの列を持つ2つの列を返します。この

... AND an.attributeid IN (113319, 1762)

ような何かを行うことができます。

行を列に転記したい場合、それは少し複雑になります。

1

これは、キー値ストアと呼ばれることもあります。あなたは、あなたが望むことをするために複数の結合でいくつかのゲームをする必要があります。

この種のものは、あなたが属性テーブルの2つのコピーを結合し、そして結合条件でそれぞれから適切な属性IDを選択してしまうトリック

SELECT whatever, 
     attr1.displayvalue attr1, 
     attr2.displayvalue attr2 
    FROM product p 
    LEFT JOIN productattribute attr1 ON p.productid = attr1.productid 
            AND attr1.attributeid = 113319 
            AND attr1.localeid = 1 
            AND attr1.isactive = 1 
    LEFT JOIN productattribute attr2 ON p.productid = attr2.productid 
            AND attr2.attributeid = 1762 
            AND attr2.localeid = 1 
            AND attr2.isactive = 1 

を行います。通常の結合ではなく、左の結合を使用するので、属性がない場合でも行が取得されます。

このデータ構成の野生の非常に一般的な例は、WordPressのwp_postmetaテーブルです。

関連する問題