Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: SQL JOINs vs WHERE statements

by CountZero (Bishop)
on Aug 18, 2003 at 21:27 UTC ( [id://284733]=note: print w/replies, xml ) Need Help??


in reply to SQL JOINs vs WHERE statements

Consider the following situation (slightly adapted from an example in the MySQL docs):

You have a staff table that contains both staff and manager's details. If you wanted a list of staff, with their corresponding manager's details as well, you would refer to the same table twice, in essence joining the table to itself. eg.
SELECT s1.StaffId, s1.StaffName, s1.Position, s1.ManagerId, s2.StaffNa +me AS ManagerName, s2.Position AS ManagerPosition FROM staff AS s1 INNER JOIN staff AS s2 ON(s1.ManagerId=s2.StaffId)

Would you be able to do that with a WHERE clause instead of a JOIN?

And how would you limit the results to all staff with position x?

CountZero

"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Replies are listed 'Best First'.
Re: Re: SQL JOINs vs WHERE statements
by demerphq (Chancellor) on Aug 18, 2003 at 22:11 UTC

    Would you be able to do that with a WHERE clause instead of a JOIN?

    SELECT stf.StaffId, stf.StaffName, stf.Position, stf.ManagerId, mgr.StaffName AS ManagerName, mgr.Position AS ManagerPosition FROM staff AS stf, staff AS mgr where stf.ManagerId=mgr.StaffId

    I think... :-)


    ---
    demerphq

    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

      (Warning: possible duplicate posting)

      Yes indeed, but the "comma" is really only a join in disguise:

      INNER JOIN and , (comma) are semantically equivalent. Both do a full join between the tables used. Normally, you specify how the tables should be linked in the WHERE condition. MySQL manual

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://284733]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-23 16:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found