[Need Help] MySQL Query

firstlord18

Erfahrenes Mitglied
Hi,

irgendwie bekomme ich die MySQL Queries grad net hin :/:

Also, meine Tabellen sehen so aus:
http://img397.imageshack.us/my.php?image=zwischenablage023yb.jpg

1. Ich will alle Gruppen auslesen, deren ID > 1 ist, und dann zu jeder Gruppe jeweils die usernamen, die in der Gruppe sind (und deren ID > 1 ist) und noch die Rechte, die die Gruppe besitzt. ABER in der Gruppe müssen nicht zwangsläufig User sein und die Gruppe muss auch nicht zwangsläufig Rechte haben ...

2. Ich will die ID und den Rechtenamen auslesen, wo die Rechteid > 1 ist. Dazu soll der Gruppenname ausgelesen werden von der Gruppe, die von dem Recht betroffen ist (soll heißen, die das Recht in der Tabelle um_groups_rights zugewießen bekommen hat). Die ID der Gruppe muss auch größer > 1 sein. Dann möchte ich noch alle usernamen (der Tabelle um_users) haben, die das Recht per um_users_rights zugewießen bekommen haben und deren ID (um_users.id) > 1 ist!


Im Moment muss ich das leider über eine Schleife lösen, da ichs nicht anders hinbekommen habe.

Ich lese dann erst alle Gruppe aus.
Dann geh ich mit ner Schleife die ID's der Gruppe durch und selektiere mit jeweils 2 (!) Queries noch a) die User, die von der Gruppe betroffen sind und b) die Rechte, die diese Gruppe hat ...
Das selbe halt mit den Rechten, da mach ichs auch mit ner Schleife.

Sind dann im Endeffekt total viele Queries (pro Gruppe bzw pro Recht 3 Queries)...

Hier noch ein DUmp (wäre dann wohl leichter für euch, das selbst zu probieren):
Code:
-- phpMyAdmin SQL Dump
-- version 2.6.2-pl1
-- http://www.phpmyadmin.net
-- 
-- Host: localhost
-- Erstellungszeit: 22. Dezember 2005 um 12:37
-- Server Version: 4.1.12
-- PHP-Version: 5.0.4
-- 
-- Datenbank: `usermanagement`
-- 

-- --------------------------------------------------------

-- 
-- Tabellenstruktur für Tabelle `um_groups`
-- 

CREATE TABLE `um_groups` (
  `id` int(10) NOT NULL auto_increment,
  `groupname` varchar(120) NOT NULL default '',
  `art` varchar(20) NOT NULL default 'by_user',
  `autogetnewright` int(2) NOT NULL default '-1',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM AUTO_INCREMENT=5 ;

-- 
-- Daten für Tabelle `um_groups`
-- 

INSERT INTO `um_groups` VALUES (1, 'admin', 'special', 1);
INSERT INTO `um_groups` VALUES (2, 'asdasd', 'special', -1);
INSERT INTO `um_groups` VALUES (3, 'gruppe1', 'special', -1);
INSERT INTO `um_groups` VALUES (4, 'gruppe2', 'special', -1);

-- --------------------------------------------------------

-- 
-- Tabellenstruktur für Tabelle `um_groups_rights`
-- 

CREATE TABLE `um_groups_rights` (
  `groups_id` int(10) NOT NULL default '0',
  `rights_id` int(10) NOT NULL default '0'
) TYPE=MyISAM;

-- 
-- Daten für Tabelle `um_groups_rights`
-- 

INSERT INTO `um_groups_rights` VALUES (1, 1);
INSERT INTO `um_groups_rights` VALUES (1, 2);
INSERT INTO `um_groups_rights` VALUES (1, 3);
INSERT INTO `um_groups_rights` VALUES (1, 4);
INSERT INTO `um_groups_rights` VALUES (1, 5);
INSERT INTO `um_groups_rights` VALUES (1, 6);
INSERT INTO `um_groups_rights` VALUES (1, 7);
INSERT INTO `um_groups_rights` VALUES (1, 8);
INSERT INTO `um_groups_rights` VALUES (1, 9);
INSERT INTO `um_groups_rights` VALUES (1, 10);
INSERT INTO `um_groups_rights` VALUES (1, 11);
INSERT INTO `um_groups_rights` VALUES (1, 12);
INSERT INTO `um_groups_rights` VALUES (1, 13);
INSERT INTO `um_groups_rights` VALUES (2, 2);
INSERT INTO `um_groups_rights` VALUES (2, 3);
INSERT INTO `um_groups_rights` VALUES (2, 4);
INSERT INTO `um_groups_rights` VALUES (2, 5);
INSERT INTO `um_groups_rights` VALUES (2, 6);
INSERT INTO `um_groups_rights` VALUES (2, 7);
INSERT INTO `um_groups_rights` VALUES (2, 8);
INSERT INTO `um_groups_rights` VALUES (2, 9);
INSERT INTO `um_groups_rights` VALUES (2, 10);
INSERT INTO `um_groups_rights` VALUES (2, 11);
INSERT INTO `um_groups_rights` VALUES (2, 12);
INSERT INTO `um_groups_rights` VALUES (2, 13);
INSERT INTO `um_groups_rights` VALUES (1, 14);
INSERT INTO `um_groups_rights` VALUES (3, 2);
INSERT INTO `um_groups_rights` VALUES (3, 3);
INSERT INTO `um_groups_rights` VALUES (3, 4);
INSERT INTO `um_groups_rights` VALUES (3, 5);
INSERT INTO `um_groups_rights` VALUES (3, 6);
INSERT INTO `um_groups_rights` VALUES (3, 7);
INSERT INTO `um_groups_rights` VALUES (3, 8);
INSERT INTO `um_groups_rights` VALUES (3, 9);
INSERT INTO `um_groups_rights` VALUES (3, 10);
INSERT INTO `um_groups_rights` VALUES (3, 11);
INSERT INTO `um_groups_rights` VALUES (3, 12);
INSERT INTO `um_groups_rights` VALUES (3, 13);
INSERT INTO `um_groups_rights` VALUES (3, 14);
INSERT INTO `um_groups_rights` VALUES (4, 8);
INSERT INTO `um_groups_rights` VALUES (4, 9);
INSERT INTO `um_groups_rights` VALUES (4, 10);

-- --------------------------------------------------------

-- 
-- Tabellenstruktur für Tabelle `um_rights`
-- 

CREATE TABLE `um_rights` (
  `id` int(10) NOT NULL auto_increment,
  `rightname` varchar(120) NOT NULL default '',
  `php_id` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM AUTO_INCREMENT=15 ;

-- 
-- Daten für Tabelle `um_rights`
-- 

INSERT INTO `um_rights` VALUES (1, 'standartrecht - do not delete!', 'default');
INSERT INTO `um_rights` VALUES (2, 'eigene einstellungen bearbeiten!', 'edit_own_prefs');
INSERT INTO `um_rights` VALUES (3, 'gruppen anlegen', 'create_groups');
INSERT INTO `um_rights` VALUES (4, 'gruppen löschen', 'delete_groups');
INSERT INTO `um_rights` VALUES (5, 'gruppen editieren', 'edit_groups');
INSERT INTO `um_rights` VALUES (6, 'user erstellen', 'create_users');
INSERT INTO `um_rights` VALUES (7, 'user löschen', 'delete_users');
INSERT INTO `um_rights` VALUES (8, 'user editierren', 'edit_users');
INSERT INTO `um_rights` VALUES (9, 'rechte erstellen', 'create_rights');
INSERT INTO `um_rights` VALUES (10, 'rechte löschen', 'delete_rights');
INSERT INTO `um_rights` VALUES (11, 'rechte editieren', 'edit_rights');
INSERT INTO `um_rights` VALUES (12, 'scripteinstellungen bearbeiten', 'edit_script_prefs');
INSERT INTO `um_rights` VALUES (13, 'datenbankbackup anlegen', 'create_db_backup');
INSERT INTO `um_rights` VALUES (14, 'asd', 'asd');

-- --------------------------------------------------------

-- 
-- Tabellenstruktur für Tabelle `um_users`
-- 

CREATE TABLE `um_users` (
  `id` int(10) NOT NULL auto_increment,
  `username` varchar(100) NOT NULL default '',
  `password` varchar(100) NOT NULL default '',
  `icq` varchar(20) NOT NULL default '',
  `mail` varchar(50) NOT NULL default '',
  `random_id` varchar(25) default '',
  `autogetnewright` int(2) NOT NULL default '-1',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM AUTO_INCREMENT=4 ;

-- 
-- Daten für Tabelle `um_users`
-- 

INSERT INTO `um_users` VALUES (1, 'admin', 'admin', '', '', '1395401740', 1);
INSERT INTO `um_users` VALUES (2, 'User2', '', '', '', '', -1);
INSERT INTO `um_users` VALUES (3, 'User3', '', '', '', '', -1);

-- --------------------------------------------------------

-- 
-- Tabellenstruktur für Tabelle `um_users_groups`
-- 

CREATE TABLE `um_users_groups` (
  `users_id` int(10) NOT NULL default '0',
  `groups_id` int(10) NOT NULL default '0'
) TYPE=MyISAM;

-- 
-- Daten für Tabelle `um_users_groups`
-- 

INSERT INTO `um_users_groups` VALUES (1, 1);
INSERT INTO `um_users_groups` VALUES (3, 3);
INSERT INTO `um_users_groups` VALUES (3, 2);
INSERT INTO `um_users_groups` VALUES (2, 4);

-- --------------------------------------------------------

-- 
-- Tabellenstruktur für Tabelle `um_users_rights`
-- 

CREATE TABLE `um_users_rights` (
  `users_id` int(10) NOT NULL default '0',
  `rights_id` int(10) NOT NULL default '0'
) TYPE=MyISAM;

-- 
-- Daten für Tabelle `um_users_rights`
-- 

INSERT INTO `um_users_rights` VALUES (1, 1);
INSERT INTO `um_users_rights` VALUES (1, 14);
INSERT INTO `um_users_rights` VALUES (3, 14);

Wäre echt klasse, wenn Ihr mir helfen könntet.
VIelen Dank im voraus!
 
Ich verstehe nicht wieso alle ihre DB-Probleme ins PHP-Forum posten wo es doch ein Datenbank-Forum gibt für solche Fragen.

greetz
daddz
 
daddz hat gesagt.:
Ich verstehe nicht wieso alle ihre DB-Probleme ins PHP-Forum posten wo es doch ein Datenbank-Forum gibt für solche Fragen.

greetz
daddz
Jo sorry, nur ich sehe hier so oft die ganzen MySQL Anfragen, und wusste nicht, dass es ein solches Forum gibt ... deswegen dachte ich, dass man die hier postet ...

Dann verschiebt den Thread bitte, oder helft mir einfach trotzdem ;)

Danke
 
daddz hat gesagt.:
Ich verstehe nicht wieso alle ihre DB-Probleme ins PHP-Forum posten wo es doch ein Datenbank-Forum gibt für solche Fragen.
Ich denke, in diesem Fall ist es gar nicht so verkehrt. Wenn man es in einem Query lösen will, was vermutlich wesentlich effizienter ist, als etliche in einer Schleife aufzurufen, benötigt man einiges an PHP-Code, um die Daten für die Ausgabe aufzubereiten.

Ein Ansatz für ein Query:
Code:
select
  um_groups.id as groupId,
  um_groups.groupname,
  um_users.id as userId,
  um_users.username,
  um_rights.id as rightId,
  um_rights.name as rightname # inkonsequente Modellierung
from um_groups
left join um_groups_rights
  on um_groups_rights.id_um_groups=um_groups.id
left join um_rights
  on um_groups_rights.id_um_rights=um_rights.id
left_join um_users_groups
  on um_users_groups.id_um_groups=um_groups.id
left_join um_users
  on um_users_groups.id_um_users=um_users.id
order by 
  um_groups.groupname ASC, 
  um_users.username ASC,
  um_rights.name ASC
Beim Auslesen musst Du Dir die Daten nun zurechtlegen:
PHP:
$tempUsers=array();
$tempRights=array();
$tempUserIds=array();
$tempRightIds=array();
$lastGroupname=array();
$lastGroupId=null;
while($row=mysql_fetch_assoc($result)) {
    if ($lastGroupId!=$row['groupId'] && $lastGroupId!=null) {
        echo "<h1>".$lastGroupname."</h1>";
        echo "<h2>Rechte</h2><ul><li>"
            .implode("</li><li>",$tempRights)
            ."</li></ul>";
        echo "<h2>User</h2><ul><li>"
            .implode("</li><li>",$tempUsers)
            ."</li></ul>";
        $tempRight=array();
        $tempRightIds=array();
        $tempUsers=array();
        $tempUserIds=array();
    }
    $lastGroupId=$row['groupId'];
    $lastGroupname=$row['groupname'];
    if($row['userId']!=null && !in_array($row['userId'],$tempUserIds) {
        $tempUsers[]=$row['username'];
        $tempUserIds[]=$row['userId'];
    }
    if($row['rightId']!=null && !in_array($row['rightId'],$tempRightIds) {
        $tempRights[]=$row['rightname'];
        $tempRightIds[]=$row['rightId'];
    }
}
Keine Garantie auf Syntaxfehlerfreiheit.

Gruß hpvw
 
Danmke für dein Ansatz...
ich aheb noch ein paar Fehler behoben, aber folgenden Fehler kann ich nicht entfernen :confused:

Also ich weiß zwar, was das heißt, aber nicht, was es für den Code bedeutet. Gab dann der Query keine ergebnisse zurück oder wie?:

Code:
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 34
  
  Notice:  Undefined index:  groupId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 47
  
  Notice:  Undefined index:  userId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 50
  
  Notice:  Undefined index:  rightId in C:\xampp\xampp\htdocs\JahLabs\User Management\query_tests.php on line 54

Danke im voraus!

Edit: habe nun noch ein paar Where Klauseln hinzugefüngt, um nur ID's > 1 zu selektieren. Naja, wenn ich print_r($row); in der Schleife mache und den Rest auskommentiere, dann die Ausgabe.

Das war nun der Query zu Problem 1 oder?

Könntest du mir eventuell noch bei 2. helfen? Danke
 
Zuletzt bearbeitet:
Ah cool, habe es nun sogar alleine hinbekommne *freu*:

PHP:
 while($row = mysql_fetch_assoc($res)) {
 
 	$k = $row['groupid'].'||'.$row['groupname'];
 	
 	if(!isset( $new[ $k ]['rights'] ) || !is_array( $new[ $k ]['rights'] ))
 		$new[ $k ]['rights'] = array();
 	if(!isset( $new[ $k ]['users'] ) || !is_array( $new[ $k ]['users'] ))	
 		$new[ $k ]['users'] = array();
 	
 	if( !in_array($row['username'], $new[ $k ]['users'] ) )
 		$new[ $k ]['users'][] = $row['username'];
 	
 	if( !in_array($row['rightname'], $new[ $k ]['rights'] ) )
 		$new[ $k ]['rights'][] = $row['rightname'];
 }

So, nun bräuchte ich noch ein bischen Hilfe bei dem "2. Punkt, also von den Rechten ausgehend die betreffenden User und Gruppen anzeigen:
2. Ich will die ID und den Rechtenamen auslesen, wo die Rechteid > 1 ist. Dazu soll der Gruppenname ausgelesen werden von der Gruppe, die von dem Recht betroffen ist (soll heißen, die das Recht in der Tabelle um_groups_rights zugewießen bekommen hat). Die ID der Gruppe muss auch größer > 1 sein. Dann möchte ich noch alle usernamen (der Tabelle um_users) haben, die das Recht per um_users_rights zugewießen bekommen haben und deren ID (um_users.id) > 1 ist!

Danke vielmals ;)
 
Der Ansatz ist im Grunde der gleiche, nur dass Du jetzt nicht von den Gruppen ausgehst, sondern von den Rechten. Du musst (fast) nur die Reihenfolge der left joins in dem Query tauschen.

Gruß hpvw
 
hpvw hat gesagt.:
Der Ansatz ist im Grunde der gleiche, nur dass Du jetzt nicht von den Gruppen ausgehst, sondern von den Rechten. Du musst (fast) nur die Reihenfolge der left joins in dem Query tauschen.

Gruß hpvw
ok danke erstmal. Frage: warum muss man die Reihenfolge überhaupt tauschen? Ist es nicht das selbe wie bei den Where abfragen, dass es egal ist, wie rum man Abfragt?
 
So, habe folgendes versucht:

PHP:
$sql = 'select
			um_rights.id as rightsid,
			um_rights.rightname,
			um_rights.php_id,
			um_users.id as userid,
			um_users.username,
			um_groups.id as groupid,
			um_groups.groupname as groupname
		from 
			um_groups
			
		left join um_groups_rights
			on um_groups_rights.groups_id=um_groups.id
		left join um_rights
			on um_groups_rights.rights_id=um_rights.id
		
		left join um_users_rights
			on um_users_rights.rights_id=um_rights.id
		left join um_users_rights
			on um_users_rights.users_id=um_users.id
			
		where
			um_groups.id > 1 AND 
			um_users.id > 1 AND 
			um_rights.id > 1
		order by 
			um_groups.groupname ASC, 
			um_users.username ASC,
			um_rights.rightname ASC';

Error #1066 => Not unique table/alias: 'um_rights'

:confused::confused:
 
firstlord18 hat gesagt.:
ok danke erstmal. Frage: warum muss man die Reihenfolge überhaupt tauschen? Ist es nicht das selbe wie bei den Where abfragen, dass es egal ist, wie rum man Abfragt?
Im left join steckt das Wörtchen left nicht ohne Grund. Die links vom left join stehende Tabelle wird anders behandelt als die rechte. Er garantiert, dass alle Datensätze aus der linken Tabelle im Ergebnis zu finden sind. Sollte sich kein Datensatz in der rechten Tabelle finden, welcher der Bedingung hinter on genügt, wird dennoch eine Ergebniszeile eingefügt und die Felder der rechten Tabelle null gesetzt. Daher ist die Reihenfolge wichtig.

firstlord18 hat gesagt.:
...
Error #1066 => Not unique table/alias: 'um_rights'

:confused::confused:
Ich würde die Rechtetabelle in das "from" setzen und mit den anderen Tabellen joinen. Wenn eine Tabelle mehrfach in einem Query abgefragt wird, muss man für sie Aliasnamen definieren, da MySQL in where-Klausen, der Projektion und on-Bedingungen sonst nicht wissen kann, auf welche der abgefragten Tabellen Du Dich beziehst.

Hier ein (sinnloses) Beispiel:
Code:
select 
  tab1.id,
  tab2.id
from tabelle as tab1
join tabelle as tab2
  on tab1.id=tab2.id
Gruß hpvw
 

Neue Beiträge

Zurück