sql >> Database >  >> RDS >> Mysql

Combineer meerdere unieke MySQL-tabellen en bestel op één kolom

Probeer dit:

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;

BEWERKEN

hoe kom ik aan het type record? (IE. of het geretourneerde resultaat afkomstig is uit de bans-tabel, de mute-tabel, de kicks-tabel enz.)

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. is het een goede gewoonte om mysql_free_result($result) te gebruiken?

  2. Het dubbele aanhalingsteken weglaten om een ​​query uit te voeren op PostgreSQL

  3. Hoe kan ik een beperking maken om te controleren of een e-mail geldig is in postgres?

  4. Werk het MYSQL-veld alleen bij als het veld null of 0 . bevat