[MySql] Left join für geteilte Benutzerkontentabellen

FBIagent

Erfahrenes Mitglied
Guten Abend,

für eine Internetpräsentz arbeite ich an einer Benutzerdatenbank, für die ich mich mit Queries für Resultate mit
Daten aus mehrerern Tabellen auseinander setzen muss. Das Query steht soweit und scheint den Anforderungen zu entsprechen. Da ich allerdings auf dem Gebiet dieser Queries relativ wenig Erfahrung habe, und die
Dokumentation von MySql für mich nicht immer ganz verständlich ist wollte ich hier einmal fragen ob dies der
richtige Weg zum Ziel ist.

Datenbankstruktur:
SQL:
CREATE TABLE `account_activation_keys` (
  `account_id` int(10) unsigned NOT NULL,
  `account_activation_key_value` char(32) NOT NULL,
  `account_activation_key_create_date` datetime NOT NULL,
  PRIMARY KEY (`account_id`),
  CONSTRAINT `fk_account_activation_keys` FOREIGN KEY (`account_id`) REFERENCES `accounts` (`account_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `account_suspendings` (
  `account_id` int(10) unsigned NOT NULL,
  `account_suspending_reason` varchar(100) NOT NULL,
  `account_suspending_create_date` datetime NOT NULL,
  PRIMARY KEY (`account_id`),
  CONSTRAINT `fk_suspended_accounts` FOREIGN KEY (`account_id`) REFERENCES `accounts` (`account_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `accounts` (
  `account_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `account_name` varchar(20) NOT NULL COMMENT 'The account name given at registration time in lower case.',
  `account_name_exact` varchar(20) NOT NULL COMMENT 'The account name given at registration time in exact spelling.',
  `account_mail` varchar(320) NOT NULL,
  `account_pwd_hash` char(32) NOT NULL COMMENT 'Account password as MD5 hash.',
  `account_access_group` enum('user','moderator','administrator') NOT NULL DEFAULT 'user',
  `account_create_date` datetime NOT NULL,
  PRIMARY KEY (`account_id`),
  UNIQUE KEY `idx_uniq_acc_name` (`account_name`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

Das Query:
SQL:
SELECT
    accounts.account_name_exact,
    account_activation_keys.account_activation_key_create_date,
    account_suspendings.account_suspending_reason,
    account_suspendings.account_suspending_create_date
FROM
    accounts
        LEFT JOIN account_activation_keys
            ON accounts.account_id = account_activation_keys.account_id
        LEFT JOIN account_suspendings
            ON accounts.account_id = account_suspendings.account_id
WHERE
    accounts.account_name = \'' . strtolower(mysql_real_escape_string($_POST['login_acc_name'], $SqlCon)) . '\' AND
    accounts.account_pwd_hash = \'' . md5($_POST['login_acc_pwd']) . '\'
LIMIT 0, 1

Die Idee ist, die Aktivierungsschlüssel und Suspendierungen von der Benutzerkontentabelle zu trennen.
Nun möchte ich, wenn ein Eintrag in den entsprechenden Tabellen existiert, dass die getrennten Daten beim Login mit als Resultat zurück kommen.
 
Zuletzt bearbeitet von einem Moderator:
Zurück