2016-07-04 14 views
0

This is my database table.はどのように一緒に

行と列の両方から異なる値を選択します。これは、いくつかのvalues.Thereと私のデータベーステーブルがtopic1からtopic8に別のトピック名です。 そして、これらのすべての値から別個のトピック名にアクセスしたいと思います。 SQLクエリにはどのようなものがありますか?

+0

もう一度実行する前に、正規化を参照してください。データベーステーブルはスプレッドシートではありません! – Strawberry

+0

@Strawberry大丈夫です。提案に感謝します。 –

+0

しかし、あなたは:-( – Strawberry

答えて

0

答えを明確にするために: UNION演算子を使用したSELECTコマンドは、それ自身が異なる値を返します。したがって、DISTINCTキーワードは必要ありません。

UNIONを使用するSELECTは、重複する値も返します。

1

ユニオンクエリを使用します。

select topic1 as topic from t union 
select topic2 as topic from t union 
select topic3 as topic from t union 
select topic4 as topic from t union 
select topic5 as topic from t union 
select topic6 as topic from t union 
select topic7 as topic from t union 
select topic8 as topic from t; 

あなたがNULLをしたくない場合は、WHERE句を含める:

select topic1 as topic from t where topic1 is not null union 
select topic2 as topic from t where topic2 is not null union 
select topic3 as topic from t where topic3 is not null union 
select topic4 as topic from t where topic4 is not null union 
select topic5 as topic from t where topic5 is not null union 
select topic6 as topic from t where topic6 is not null union 
select topic7 as topic from t where topic7 is not null union 
select topic8 as topic from t where topic8 is not null; 
+0

をチェックしないと、あなたの驚異的な評判だけでは、この回答(とそのデータモデル)が何らかの形でRDBMSの中で適切であるという印象をOPに与えることができます。私がそれが単なる完璧な記録の収差であることをよく知らないのです。 – Strawberry

+0

それは働いています.thanks @Gordon Linoff –

0

はこれを試してください:すべてのための

SELECT DISTINCT topic1 
FROM TABLE 
UNION 
SELECT DISTINCT topic2 
FROM TABLE 
SELECT DISTINCT topic3 
FROM TABLE 

繰り返しを必要な列

関連する問題