sql >> Databasteknik >  >> RDS >> Mysql

Kombinera flera unika MySQL-tabeller och ordna efter en kolumn

Prova detta:

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;

REDIGERA

hur kunde jag få tag i posten? (IE. om det returnerade resultatet är från bans-tabellen, mutes-tabellen, kick-tabellen 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. Korrekt indexering av Join-Where-Group Genom att välja frågor undviker du att använda temporär; Använder filsortering

  2. Ändra avgränsaren till ett kommatecken när du skickar frågeresultat via e-post i SQL Server (T-SQL)

  3. Hur får man fram kolumnnamnen på tabellen i en databas i zend?

  4. MySQL-ranking i fallet med slipsar