dongxiusuo9881 2013-12-12 03:58
浏览 116
已采纳

组合多个唯一的MySQL表并按一列排序

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:

mb_bans:
mb_bans

mb_ban_records:
mb_ban_records

mb_kicks:
mb_kicks

mb_mutes:
mb_mutes

mb_mutes_records:
mb_mutes_records

mb_warnings:
mb_warnings

What I'm trying to accomplish is something among the lines of this:
End Result

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..

$banq = $db->prepare('SELECT banned, banned_by, ban_reason, ban_time, ban_expires_on FROM '.BAN_TABLE.' ORDER BY ban_time DESC');
$kickq = $db->prepare('SELECT kicked, kicked_by, kick_reason, kick_time FROM '.KICK_TABLE.' ORDER BY kick_time DESC');
$muteq = $db->prepare('SELECT muted, muted_by, mute_reason, mute_time, mute_expires_on FROM '.MUTE_TABLE.' ORDER BY mute_time DESC');
$warnq = $db->prepare('SELECT warned, warned_by, warn_reason, warn_time FROM '.WARN_TABLE.' ORDER BY warn_time DESC');
$banq->execute();
$kickq->execute();
$muteq->execute();
$warnq->execute();

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.

Thanks in advance!

  • 写回答

2条回答 默认 最新

  • douganmo1121 2013-12-13 18:26
    关注

    Try this:

    SELECT * from (
    SELECT banned as Punisher, banned_by as Punished, ban_reason as Reason, ban_expires_on  as Expire, ban_time as Date  FROM mb_bans 
    UNION
    SELECT kicked as Punisher, kicked_by as Punished, kick_reason as Reason, NULL  as Expire, kick_time as Date FROM mb_kicks 
    UNION
    SELECT muted as Punisher, muted_by as Punished, mute_reason as Reason, mute_expires_on  as Expire, mute_time as Date  FROM mb_mutes 
    UNION
    SELECT warned as Punisher, warned_by as Punished, warn_reason as Reason, NULL  as Expire,  warn_time as Date FROM mb_warnings
    ) d order by d.Date DESC;
    

    EDIT

    how could I get the type of record? (IE. whether the returned result is from the bans table, mutes table, kicks table etc.)

    SELECT * from (
        SELECT banned as Punisher, banned_by as Punished, ban_reason as Reason, ban_expires_on  as Expire, 'ban' as TableType, ban_time as Date  FROM mb_bans 
        UNION
        SELECT kicked as Punisher, kicked_by as Punished, kick_reason as Reason, NULL  as Expire, 'kick' as TableType, kick_time as Date FROM mb_kicks 
        UNION
        SELECT muted as Punisher, muted_by as Punished, mute_reason as Reason, mute_expires_on  as Expire, 'mute' as TableType, mute_time as Date  FROM mb_mutes 
        UNION
        SELECT warned as Punisher, warned_by as Punished, warn_reason as Reason, NULL  as Expire,  'warn' as TableType, warn_time as Date FROM mb_warnings
        ) d order by d.Date DESC;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记