Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL large relational query
    primarykey
    data
    text
    <p>I have a few tables:</p> <p><strong>letter_mail</strong> </p> <pre><code>index sent from to template public stamp stationery title content opened </code></pre> <p><strong>letter_user</strong> </p> <pre><code>index username password </code></pre> <p>ALL the rows in letter_mail are relational, except for index,public and opened, to another table.</p> <p>From and To in letter_mail corresponds to the index of letter_user. What i want is to pull all the data from the database, preferably in one query if possible. A * Select on a <code>letter_mail</code> row will yield a result much like this:</p> <pre><code>index:1 sent: 2013-10-03 from:1 to:2 template:1 public:1 stamp:1 stationery:1 title: 1 content: 1 opened : 0 </code></pre> <p>What i need, is for the information above to be filled with data from the related tables and be <code>JSON</code> encoded. And look somewhat like this:</p> <pre><code>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 </code></pre> <p>Is this completely crazy? Should i break the query into several bits or maybe collate everything into one table all together?</p> <p>Please tell if you need more information :)</p> <p><strong>Solution is this</strong>:</p> <pre><code>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 </code></pre> <p>The query works, but doesn't return any rows.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload