PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > with 3 tables select
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
with 3 tables select

Réponse
 
LinkBack Outils de la discussion
Vieux 05/11/2007, 21h24   #1
FFMG
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut with 3 tables select


Hi,

I have a table of users

TABLE_A = username, userID

They are member of one _or more_ group

TABLE_B = userID, grouID

Each group has a set of one _or more_ permissions

TABLE_C = grouID, permission

So to get the permissions for one user I could do 3 queries.

SELECT userID FROM TABLE_A where username = 'x'
SELECT groupID FROM TABLE_B where userID = TABLE_A.userID
SELECT permission FROM TABLE_C where groupID = TABLE_B.groupID

How could I do the above in one simple query?

The table are really that simple, but I want to keep them separated
because each groupID will have a name and each permissionID will also
have a name.

How would you simplify those 3 queries?

Thanks
FFMG


--

'webmaster forum' (http://www.httppoint.com) | 'Free Blogs'
(http://www.journalhome.com/) | 'webmaster Directory'
(http://www.webhostshunter.com/)
'Recreation Vehicle insurance'
(http://www.insurance-owl.com/other/car_rec.php) | 'Free URL
redirection service' (http://urlkick.com/)
------------------------------------------------------------------------
FFMG's Profile: http://www.httppoint.com/member.php?userid=580
View this thread: http://www.httppoint.com/showthread.php?t=21905

Message Posted via the webmaster forum http://www.httppoint.com, (Ad revenue sharing).

  Réponse avec citation
Vieux 06/11/2007, 08h47   #2
Pavel Lepin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: with 3 tables select


FFMG <FFMG.2zlyes@no-mx.httppoint.com> wrote in
<FFMG.2zlyes@no-mx.httppoint.com>:
> I have a table of users
>
> TABLE_A = username, userID
>
> They are member of one _or more_ group
>
> TABLE_B = userID, grouID
>
> Each group has a set of one _or more_ permissions
>
> TABLE_C = grouID, permission
>
> So to get the permissions for one user...


....you should use JOINs. Read about JOIN syntax in MySQL
Reference Manual.

--
"I can't but wonder if you... don't know a hell of a
lot more about practically every subject than Solomon ever
did."
  Réponse avec citation
Vieux 06/11/2007, 09h42   #3
FFMG
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: with 3 tables select


Pavel Lepin;101603 Wrote:
>
> ....you should use JOINs. Read about JOIN syntax in MySQL
> Reference Manual.
>
> --
> "I can't but wonder if you... don't know a hell of a
> lot more about practically every subject than Solomon ever
> did."


Thanks, I did look at JOINS

SELECT * from TABLE_A
join TABLE_B on TABLE_A.userId = TABLE_B.userId
join TABLE_C on TABLE_B.groupId = TABLE_C.groupId
WHERE
TABLE_A.username = 'x'

But I am not sure this is the most efficient way of doing it.

FFMG


--

'webmaster forum' (http://www.httppoint.com) | 'Free Blogs'
(http://www.journalhome.com/) | 'webmaster Directory'
(http://www.webhostshunter.com/)
'Recreation Vehicle insurance'
(http://www.insurance-owl.com/other/car_rec.php) | 'Free URL
redirection service' (http://urlkick.com/)
------------------------------------------------------------------------
FFMG's Profile: http://www.httppoint.com/member.php?userid=580
View this thread: http://www.httppoint.com/showthread.php?t=21905

Message Posted via the webmaster forum http://www.httppoint.com, (Ad revenue sharing).

  Réponse avec citation
Vieux 06/11/2007, 10h45   #4
FFMG
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: with 3 tables select


Pavel Lepin;101603 Wrote:
>
>
> ....you should use JOINs. Read about JOIN syntax in MySQL
> Reference Manual.
>
> --
> "I can't but wonder if you... don't know a hell of a
> lot more about practically every subject than Solomon ever
> did."


Sorry for the double reply.

But why would a JOIN be better than a multiple select?

I could so

SELECT TABLE_C.* FROM TABLE_A, TABLE_B, TABLE_B
WHERE
TABLE_C.permission = TABLE_B.permission
AND
TABLE_B.userID = TABLE_A.userID
AND
TABLE_A.username = 'x'

And in my case get the same result, what would be the 'better' way of
selecting the permissions?

FFMG


--

'webmaster forum' (http://www.httppoint.com) | 'Free Blogs'
(http://www.journalhome.com/) | 'webmaster Directory'
(http://www.webhostshunter.com/)
'Recreation Vehicle insurance'
(http://www.insurance-owl.com/other/car_rec.php) | 'Free URL
redirection service' (http://urlkick.com/)
------------------------------------------------------------------------
FFMG's Profile: http://www.httppoint.com/member.php?userid=580
View this thread: http://www.httppoint.com/showthread.php?t=21905

Message Posted via the webmaster forum http://www.httppoint.com, (Ad revenue sharing).

  Réponse avec citation
Vieux 06/11/2007, 12h44   #5
Pavel Lepin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: with 3 tables select


FFMG <FFMG.2zmfl0@no-mx.httppoint.com> wrote in
<FFMG.2zmfl0@no-mx.httppoint.com>:
> Pavel Lepin Wrote:
>> ....you should use JOINs. Read about JOIN syntax in MySQL
>> Reference Manual.

>
> But why would a JOIN be better than a multiple select?


"Multiple select?"

> SELECT TABLE_C.* FROM TABLE_A, TABLE_B, TABLE_B
> WHERE
> TABLE_C.permission = TABLE_B.permission
> AND
> TABLE_B.userID = TABLE_A.userID
> AND
> TABLE_A.username = 'x'


You haven't read the chapter on JOINs, did you? I'll quote
from MySQL 5.0 Reference Manual, 12.2.7.1 for you:

INNER JOIN and , (comma) are semantically equivalent in
the absence of a join condition: both produce a Cartesian
product between the specified tables (that is, each and
every row in the first table is joined to each and every
row in the second table).

So in a very real sense, this query still uses JOINs. You're
simply using WHERE clause instead of join condition to
filter the result set.

> And in my case get the same result, what would be the
> 'better' way of selecting the permissions?


The result set will be the same. Some people find the syntax
with explicit JOINs and join conditions less obscure and
more to the point. YMMV. I remember a reasonably
knowledgeable person saying mysqld might have a harder time
optimising queries using WHERE clause instead of join
conditions, but I'm not a query optimisation expert myself,
so don't quote me on that. If in doubt, run benchmarks.

--
"I can't but wonder if you... don't know a hell of a
lot more about practically every subject than Solomon ever
did."
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h03.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11711 seconds with 13 queries