sql >> データベース >  >> RDS >> Mysql

複数の一意のMySQLテーブルを組み合わせて、1つの列で並べ替えます

    これを試してください:

    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;
    

    編集

    どのようにしてレコードの種類を取得できますか? (つまり、返された結果が禁止テーブル、ミュートテーブル、キックテーブルなどからのものであるかどうか)

    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. PHPでmysqlの日付をAgoに変更する方法

    2. SQL Server 2016のSTRING_SPLIT():フォローアップ#2

    3. MySQLのselectクエリで使用するインデックスをヒントにする方法は?

    4. 入力配列に基づいてUPSERTする機能の改善