Note that there are some explanatory texts on larger screens.

plurals
  1. POCombine multiple unique MySQL tables and order by one column
    text
    copied!<p>I've been trying to accomplish this MySQL query for the past few days now with very little luck. I'd like to combine these multiple tables and their columns, then order by one that they have in common (not by name, but by content). I have the following database tables:</p> <p>mb_bans:<br> <img src="https://i.stack.imgur.com/LvZ6X.png" alt="mb_bans"></p> <p>mb_ban_records:<br> <img src="https://i.stack.imgur.com/7WG80.png" alt="mb_ban_records"></p> <p>mb_kicks:<br> <img src="https://i.stack.imgur.com/EwyXf.png" alt="mb_kicks"></p> <p>mb_mutes:<br> <img src="https://i.stack.imgur.com/ZOI3Q.png" alt="mb_mutes"></p> <p>mb_mutes_records:<br> <img src="https://i.stack.imgur.com/JxSa7.png" alt="mb_mutes_records"></p> <p>mb_warnings:<br> <img src="https://i.stack.imgur.com/bMnmy.png" alt="mb_warnings"></p> <p>What I'm trying to accomplish is something among the lines of this:<br> <img src="https://i.stack.imgur.com/JrrTh.png" alt="End Result"></p> <p>Unfortunately I am beyond stumped on how to combine these MySQL tables together but also at the same time keeping them in separate categories - ordered by the _time columns in each table. How would I approach this? I have been unsuccessful with my attempts at retrieving them.. The closest I could get was combining just the _time columns of each, then giving it a value in the query as "date", however I cannot do much with the results. I would still need to call it as the individual rows, correct? I could probably use fetchAll but then I would be unable to add anything to the values..</p> <pre><code>$banq = $db-&gt;prepare('SELECT banned, banned_by, ban_reason, ban_time, ban_expires_on FROM '.BAN_TABLE.' ORDER BY ban_time DESC'); $kickq = $db-&gt;prepare('SELECT kicked, kicked_by, kick_reason, kick_time FROM '.KICK_TABLE.' ORDER BY kick_time DESC'); $muteq = $db-&gt;prepare('SELECT muted, muted_by, mute_reason, mute_time, mute_expires_on FROM '.MUTE_TABLE.' ORDER BY mute_time DESC'); $warnq = $db-&gt;prepare('SELECT warned, warned_by, warn_reason, warn_time FROM '.WARN_TABLE.' ORDER BY warn_time DESC'); $banq-&gt;execute(); $kickq-&gt;execute(); $muteq-&gt;execute(); $warnq-&gt;execute(); </code></pre> <p>Essentially I'd like to combine all of those queries together as one + the two _record tables. Any advice that could help me is greatly appreciated as I've spent countless hours trying to figure this out on my own.</p> <p>Thanks in advance!</p>
 

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