Welcome to Dream.In.Code
Become an Expert!

Join 149,430 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,304 people online right now. Registration is fast and FREE... Join Now!




Join

2 Pages V  1 2 >  
Reply to this topicStart new topic

Join

fyrestorm
6 Jul, 2002 - 06:22 AM
Post #1

D.I.C Lover
Group Icon

Joined: 4 Apr, 2002
Posts: 3,103



Thanked: 2 times
Dream Kudos: 228
My Contributions
how do i use JOIN in mysql?
User is offlineProfile CardPM
+Quote Post

gneato
RE: Join
6 Jul, 2002 - 11:05 AM
Post #2

<title>Untitled Document</title>
*****

Joined: 3 Sep, 2001
Posts: 1,311

http://www.mysql.com/doc/J/O/JOIN.html
User is offlineProfile CardPM
+Quote Post

fyrestorm
RE: Join
6 Jul, 2002 - 12:24 PM
Post #3

D.I.C Lover
Group Icon

Joined: 4 Apr, 2002
Posts: 3,103



Thanked: 2 times
Dream Kudos: 228
My Contributions
ok, i went there and read that before i posted this question, and it didn't really tell me how to do a regular join...it went into left join and then right joins more than anything else...

but it's all good, chris explained it to me smile.gif
User is offlineProfile CardPM
+Quote Post

RE: Join
6 Jul, 2002 - 12:31 PM
Post #4




Joined: --
Posts: 0

The key to JOINS is basically this:


SQL
"SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.secondarykey WHERE table2.type = $var"


it allows you to select items from the first table based upon a reference in the second table. In this case you would pull back all the "things" in table 1 that were associated with the type you specify in table2.

here is a madeupexample that isn't spectacular but does demonstrate the power:
Say we wanted to pull back all the red items of clothing from a shopping cart -

you have a table called product and a table called option -
the table option has 4 fields id, productid, color, type

SQL
"SELECT * FROM product LEFT JOIN option ON product.id = option.productid WHERE option.color = 'red'"


Hope this helps, sorry I didn't get to answer you better at work,
H
User is offlinePM
+Quote Post

fyrestorm
RE: Join
6 Jul, 2002 - 12:32 PM
Post #5

D.I.C Lover
Group Icon

Joined: 4 Apr, 2002
Posts: 3,103



Thanked: 2 times
Dream Kudos: 228
My Contributions
oh no, you're good, you told me what i needed to know, i just didn't know how to do it, but i don't think that's going to work anyways...

but i do appreciate the explanation
User is offlineProfile CardPM
+Quote Post

RE: Join
6 Jul, 2002 - 12:59 PM
Post #6




Joined: --
Posts: 0

why won't it work?

If the data is not "relational" then it won't work, but that's a database design issue. The real power of "relational databases" is in the relational part. other than that you have a slightly more efficient method of storing data than a flat file system. And it's only more efficient from the standpoint of indexing because the file size is probably very similar between data in text files and all the taxt file roled up into a table. If either is smaller it might be the flat files...

H
User is offlinePM
+Quote Post

fyrestorm
RE: Join
7 Jul, 2002 - 04:05 PM
Post #7

D.I.C Lover
Group Icon

Joined: 4 Apr, 2002
Posts: 3,103



Thanked: 2 times
Dream Kudos: 228
My Contributions
it won't work because the data isn't relational, it's two separate entities, it's like a tortise and a hare, they're are completely and totally different

besides, i'm not looking to put the tables side by side, i want one on top of the other

and further more, both tables contain the some of the same fields, and those are the ones that i want to look at, would't that cause a problem if i put the tables side by side?
User is offlineProfile CardPM
+Quote Post

malkiri
RE: Join
7 Jul, 2002 - 07:49 PM
Post #8

D.I.C Regular
Group Icon

Joined: 29 Mar, 2001
Posts: 364



Thanked: 1 times
Dream Kudos: 4
My Contributions
I think what you're looking for is the UNION operation. As in:
CODE
SELECT mycolumn1, mycolumn2
FROM table1
UNION
SELECT thatcolumn1, thatcolumn2
FROM table2

You can continue in the same manner and UNION several queries together. All the types must match, and there must be the same number of columns in each query. The column names for the result are the names of the columns in the first query.
User is offlineProfile CardPM
+Quote Post

RE: Join
8 Jul, 2002 - 07:28 AM
Post #9




Joined: --
Posts: 0

Hmmm,

Not sure I follow you, when you say one on top of the other as opposed to side by side are you talking about output/display?

If you want to look at the fields that are the same, JOINS are one way you can do it. Since nested SELECT is not supported yet in MySQL you can also do a query on one table and build a comma delim list to check against the other table. To many ifs in my mind, I think I need an example of the data.

Why do both tables have the same fields?

H
User is offlinePM
+Quote Post

skyhawk133
RE: Join
8 Jul, 2002 - 08:04 AM
Post #10

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 15,253



Thanked: 60 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
I follow what she says but don't know if there is a way to do it with one query... I think the easiest thing would be to put the usernames and passwords back into 1 table and using an identifier to tell the script to pull from clients or users based on the identifier. Having 2 seperate tables and only 1 login form is a pain in the butt because you do undoubtedly have to do 2 seperate queries. In the end, since the script you are working on is not going to contain an exhorbanant amount of data; having 2 queries is minimal in my eyes. But of course doing it the right way is always nice smile.gif
User is online!Profile CardPM
+Quote Post

RE: Join
8 Jul, 2002 - 08:13 AM
Post #11




Joined: --
Posts: 0

You follow because you have seen the data... cool.gif or at least talked to her about it...

I would reccomend moving the login's to one table because doing it right is almost always better and it probably won't be that hard at this point. If the scope is small it probably doesn't matter much but things have a way of growing online and you can't always be sure that it will remain small.

On the other hand it's very possible you can log into both tables with one form by using an AND statement but then you'll be blocking entry to those who have only registered in one table... (this assumes I am understanding the issue from Chris' post)

H
User is offlinePM
+Quote Post

skyhawk133
RE: Join
8 Jul, 2002 - 08:25 AM
Post #12

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 15,253



Thanked: 60 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
What if you did an OR...

SQL
SELECT client_username.clients, client_password.clients, team_password.team, team_username.team FROM clients, team WHERE client_username.clients = '$username' OR team_username.team = '$username';


then when you pull back your data you can do an if statement to see if client_username is blank if it is you know you need to check against the team variable, if it's not you need to check against the client variable.

Kinda a messed up way to do it, but I think that's what hoopster was trying to say with using if statements?!
User is online!Profile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:42AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month