View Full Version : Selecting from multiple tables.
Halide
08-24-2002, 10:24 PM
Ok, I'm not very experienced with SQL yet, everything I do know is self-taught. Anywho, where's a good place to learn about selecting from multiple tables.
And can you "link" fields like in MS Access?
sans-hubris
08-26-2002, 02:36 PM
What exactly are you trying to do? Might you not wish to do a JOIN?
_underdog
08-26-2002, 03:37 PM
www.w3schools.com has a good SQL tutorial.
Halide
08-26-2002, 05:45 PM
not sure about JOIN? :) I'll check out the tutorial, thanks
Halide
08-26-2002, 05:53 PM
hmm i took the quiz and i got 100% but the questions were way too easy
Halide
08-26-2002, 05:59 PM
these join's are kind of confusing...
i'm not sure if i can do what i want with it?
I have a table names forum_topics and forum_replies
forum_replies has a field specifying the topic id, id from forum_topics is the same
so can i select posts from both tables using an INNER JOIN?
iDxMan
08-27-2002, 07:25 PM
What have you tried so far?
SELECT *
FROM table1 as a, table2 as b
WHERE a.topic_id = 'var_goes_here'
AND a.topic_id = b.topic_id
Halide
08-27-2002, 07:50 PM
I haven't tried anything yet, cause I'm not sure I want to mess up my code... although it would be better that way?
do you not need any join code in the statement?
Grizzly
08-27-2002, 08:04 PM
The "join code" is taking place on the line which says:
AND a.topic_id = b.topic_id
That statement establishes a relationship between table (a) and table (b). This is the true-blue, standard way of joining tables.
The "JOIN" statement you're looking for is more or less a Microsoft invention, and therefore it generally only works on MS databases (MSSQL, Access) I think a few other DBMS vendors have adopted it though.
Halide
08-27-2002, 08:39 PM
ok does this look reasonable:
SELECT title, text
FROM forum_topics, forum_replies
WHERE forum_topics.id = <TOPICID>
AND forum_topics.id = forum_replies.topic_id
I have no idea what the "AS blah" is needed for?
gufmn
08-27-2002, 09:27 PM
Originally posted by Halide
I have no idea what the "AS blah" is needed for? [/B]
It's so you don't have to keep typing table1 or table2. By using AS, you can just call them by whatever you aliased them AS.
For instance:
a.topic_id
instead of:
table1.topic_id
Not really needed if you're writing minimal statements or if the table names are short and easy to remember. But, I'm sure you can see how it could be usefull.
Halide
08-27-2002, 09:52 PM
ok I'll try it :)
iDxMan
08-28-2002, 12:22 AM
Originally posted by gufmn
Not really needed if you're writing minimal statements or if the table names are short and easy to remember. But, I'm sure you can see how it could be usefull.
For us lazy folk. :) Actually I do it all the time, but its really handy when you have long table names.
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.