If we are wanting to pull information from two different tables that are linked in some way, the conventional way of doing this is as follows:
SELECT table1.item, table2.item
FROM table1 JOIN table2 ON table1.item = table2.item;
However an alternative is to use the following;
SELECT table1.item, table2.item
FROM table1, table2
WHERE table1.item = table2.item;
If we use the following as an example;
users has a PK username and log has a PK id
log also has a number of FKs but the one we are interested in here is username which of course relates/points to the username field in users.
So with the above in mind, if we are looking to query the information in the log table then we can do so with the following;
SELECT id, fault_occured, machine, `module`, fault, responsibility,
resolved, username, fault_resolved FROM log;
But let's say we want to see the realname instead of the username. We then need to grab the realname from the users table and we can do this in one of two ways;
Using a JOIN in either of the following;
SELECT id, fault_occured, machine, `module`, fault, responsibility,
resolved, users.realname, fault_resolved
FROM log JOIN users ON log.username=users.username ORDER BY fault_occured DESC;
the below is the same only instead of using JOIN ON, we use JOIN WHERE
SELECT id, fault_occured, machine, `module`, fault, responsibility,
resolved, users.realname, fault_resolved
FROM log JOIN users WHERE log.username=users.username;
or alternatively just using plain old WHERE with no JOIN
SELECT id, fault_occured, machine, `module`, fault, responsibility,
resolved, users.realname, fault_resolved
FROM log,users WHERE log.username=users.username;
No comments:
Post a Comment
Note: only a member of this blog may post a comment.