我有几张桌子:
letter_mail
index
sent
from
to
template
public
stamp
stationery
title
content
opened
信用户
index
username
password
letter_mail 中的所有行都是与另一个表相关的,除了 index、public 和 opens。
letter_mail 中的 From 和 To 对应于 letter_user 的索引。我想要的是从数据库中提取所有数据,如果可能的话,最好在一个查询中。A * Select on a letter_mail
row 将产生如下结果:
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
这完全疯了吗?我应该将查询分成几位还是将所有内容一起整理到一张表中?
请告诉您是否需要更多信息:)
解决方案是这样的:
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
查询有效,但不返回任何行。