私はいくつかのテーブルを持っています:
letter_mail
index
sent
from
to
template
public
stamp
stationery
title
content
opened
letter_user
index
username
password
index、public、opened を除いて、letter_mail のすべての行は、別のテーブルと関係があります。
letter_mail の From と To は、letter_user のインデックスに対応します。私が望むのは、データベースからすべてのデータを、可能であれば 1 つのクエリで取得することです。行を* 選択すると、letter_mail
次のような結果が得られます。
index:1
sent: 2013-10-03
from:1
to:2
template:1
public:1
stamp:1
stationery:1
title: 1
content: 1
opened : 0
私が必要とするのは、上記の情報に関連するテーブルからのデータを入力してJSON
エンコードすることです。そして、次のようになります。
index:1
sent: 2013-10-03
from: {1, John}
to: {2, Jane}
template: {index: 1, template: "standard template", url: "template_name"}
public: 0
stamp: {index: 1, stamp: "standard stamp", url: "some/url"}
stationery: {index: 1, stamp: "standard stationery", url: "some/url"}
title: {index: 1, title: "some title"}
content: {index: 1, content: "some text content"}
opened : 0
これは完全にクレイジーですか?クエリをいくつかのビットに分割するか、すべてを 1 つのテーブルにまとめて照合する必要がありますか?
さらに情報が必要な場合はお知らせください:)
解決策はこれです:
select
mail.index,
mail.sent,
mail.opened,
mail.public,
FromU.username as FromUser,
ToU.username as ToUser,
T.template as TemplateName,
T.url as TemplateURL,
S.stamp,
S.url as StampURL,
S.stamp Stamp,
STA.url StationaryURL,
Ttl.title,
C.content
from
letter_mail mail
JOIN letter_user FromU
on mail.from = FromU.index
JOIN letter_user ToU
on mail.to = ToU.index
JOIN letter_templates T
on mail.template = T.index
JOIN letter_stamps S
on mail.stamp = S.index
JOIN letter_stationery STA
on mail.stationery = STA.index
JOIN letter_title Ttl
on mail.title = Ttl.index
JOIN letter_content C
on mail.content = C.index
クエリは機能しますが、行が返されません。