0

NameAddressPhoneNameAddressAge、という名前の 3 つのテーブルを持つデータベースがありますAgeSex

  • テーブルNameAddressPhoneには列nameaddress、およびがありphoneます。
  • テーブルNameAddressAgeには列nameaddress、およびがありageます。
  • テーブルAgeSexには列ageとがありますsex

名前と住所が と の両方に表示され、年齢が と の両方にNameAddressPhone表示されるように、名前、住所、年齢を検索する (SQLite) クエリを作成しようとしています。を使用して途中まで (つまり、2 つのテーブルで) 到達することはできますが、私は SQL に手を出すだけであり、これを正しく行うための専門家の助けをいただければ幸いです。似ているように見える解決策を見たことがありますが、その論理には完全に従っていません。NameAddressAgeNameAddressAgeAgeSexinner join

前もって感謝します。

クリス

4

2 に答える 2

1

I am assuming your tables have the following layout

NameAddressPhone
================
Name
Address
Phone

NameAddressAge
==============
Name
Address
Age

AgeSex
======
Age
Sex

If I am understanding everything correctly, the solution might look kind of like this:

SELECT P.Name, P.Address, P.Phone, A.Age, S.Sex
FROM NameAddressPhone P
INNER JOIN NameAddressAge A ON P.Name = A.Name AND P.Address = A.Address
INNER JOIN AgeSex S ON A.Age = S.Age

Mind you, joining AgeSex could produce duplicate rows if there are multiple rows with the same age in AgeSex. There wouldn't be a way to distinguish 21 and Male from 21 and Female, for example.

I hope I can help and this is what you are looking for.

于 2013-06-17T21:02:17.290 に答える
1

明らかなキーでこれらを結合したいだけだと思います:

select *
from NameAddressPhone nap join
     NameAddressAge naa
     on nap.name = naa.name and
        nap.address = naa.address join
     (select distinct age
      from AgeSex asx
     ) asx
     on asx.age = naa.age

AgeSexこれは、行の増殖を防ぐために、 で異なる年齢を選択しています。おそらく、1 つの age がそのテーブルに複数回出現する可能性があり、その結果、出力で行が重複することになります。

于 2013-06-17T21:00:45.817 に答える