MyBB Community Forums

Full Version: MySQL "JOIN" queries[HELP]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
(2009-02-10, 11:34 PM)D-Fraz Wrote: [ -> ]I know:
Echos (Of course :p )
Variables
Sessions
Cookies
Syntax
Strings
Operators
Comments
If
Else
Elseif
Require
Include
Arrays
While Loop
Functions
Post
Get
HTML Entities
str_replace
Date

As for mysql:

Select
Insert
Delete
Connect
Queries
Fetch Array
Update
Join
Count
Group by
SQL Injection
and more.
NiceSmile I don't know the JOIN's yet, trying to find a good tutorial or someone to explain it to me. Other than that, I know all of what you know - plus OOP. Not to mention I know HTML, CSS, AJAX, CSS, and Prototype+jQuery(javascript libraries)
(2009-02-10, 11:48 PM)Rcpalace Wrote: [ -> ]
(2009-02-10, 11:38 PM)TomL Wrote: [ -> ]NiceSmile I don't know the JOIN's yet, trying to find a good tutorial or someone to explain it to me. Other than that, I know all of what you know - plus OOP. Not to mention I know HTML, CSS, AJAX, CSS, and Prototype+jQuery(javascript libraries)

Join is just an alias of implode, so if you know implode, you know join. A detailed tutorial isn't necessary IMO, it's just a function and you can learn how to use it on php's website.

Erm - I think he's talking SQL wise.

TomL, JOIN allows you to select two or more tables in one query for information that is needed from both. For instance I would want to join the users table to the posts table to check if the username used in the post is a guests or not.
(2009-02-11, 04:05 AM)Ryan Gordon Wrote: [ -> ]
(2009-02-10, 11:48 PM)Rcpalace Wrote: [ -> ]
(2009-02-10, 11:38 PM)TomL Wrote: [ -> ]NiceSmile I don't know the JOIN's yet, trying to find a good tutorial or someone to explain it to me. Other than that, I know all of what you know - plus OOP. Not to mention I know HTML, CSS, AJAX, CSS, and Prototype+jQuery(javascript libraries)

Join is just an alias of implode, so if you know implode, you know join. A detailed tutorial isn't necessary IMO, it's just a function and you can learn how to use it on php's website.

Erm - I think he's talking SQL wise.

TomL, JOIN allows you to select two or more tables in one query for information that is needed from both. For instance I would want to join the users table to the posts table to check if the username used in the post is a guests or not.
Yes, I was talking SQL wise.

Well, I get that much - but I don't get the "context" of it. Like, I looked at one of the queries in MyBB and got completely confused.
(2009-02-11, 04:05 AM)Ryan Gordon Wrote: [ -> ]Erm - I think he's talking SQL wise.

Ah, that makes more sense now. Toungue

Quote:but I don't get the "context" of it.

TomL, basically, it allows you to compare or pull data from two or more tables in one query. Let's say I have a table called Jobs and another called Job_categories. The Jobs table contains a list of jobs and possible categories. In the job_categories table, I have an entire list of job categories.

The structure of the two tables:

Jobs
-jid --> ID
-name --> Job name
-p_cat --> Possible Category

Job_categories
->cid --> ID
->category --> Category

Now, in order to show all jobs based on categories from Job_categories, we can do this:

$query = "SELECT * ALL FROM Jobs JOIN job_categories WHERE jobs.p_cat = job_categories.category";

In order for MySQL to understand which table you're referring to when looking at columns make sure you add the tablename. in front of the column as shown above in "jobs.p_cat" and "job_categories.category". You can make prefixes for the tables if they're long using "AS" and prefix.
Hope this helps,
Best Regards.
(2009-02-11, 04:36 AM)Rcpalace Wrote: [ -> ]
(2009-02-11, 04:05 AM)Ryan Gordon Wrote: [ -> ]Erm - I think he's talking SQL wise.

Ah, that makes more sense now. Toungue

Quote:but I don't get the "context" of it.

TomL, basically, it allows you to compare or pull data from two or more tables in one query. Let's say I have a table called Jobs and another called Job_categories. The Jobs table contains a list of jobs and possible categories. In the job_categories table, I have an entire list of job categories.

The structure of the two tables:

Jobs
-jid --> ID
-name --> Job name
-p_cat --> Possible Category

Job_categories
->cid --> ID
->category --> Category

Now, in order to show all jobs based on categories from Job_categories, we can do this:

$query = "SELECT * ALL FROM Jobs JOIN job_categories WHERE jobs.p_cat = job_categories.category";

In order for MySQL to understand which table you're referring to when looking at columns, make sure you add the tablename. in front of the column as shown above in "jobs.p_cat" and "job_categories.category". You can make prefixes for the tables if they're long using "AS" and prefix.
Hope this helps,
Best Regards.
So, just to make sure I understand.

Say for example, on a forum; you have a table threads, and thread_posts

threads
-> id(the id of the thread)
-> title
-> posted
-> poster
-> fID(the forum to show it in)

thread_posts
-> id(the ID of the post)
-> title
-> posted
-> posted
-> tID(the thread)

$query = "SELECT * ALL FROM threads JOIN thread_posts WHERE threads.tID = thread_posts.id";
You actually got it a bit backward, this:


$query = "SELECT * ALL FROM threads JOIN thread_posts WHERE threads.tID = thread_posts.id"; 

Should be this:

$query = "SELECT * FROM thread_posts JOIN threads WHERE thread_posts.tid = threads.id"; 
(2009-02-11, 04:46 AM)Rcpalace Wrote: [ -> ]You actually got it a bit backward, this:


$query = "SELECT * ALL FROM threads JOIN thread_posts WHERE threads.tID = thread_posts.id"; 

Should be this:

$query = "SELECT * FROM thread_posts JOIN threads WHERE thread_posts.tid = threads.id"; 
Ah. I understand now, but then once I run that query - how could I differentiate the two?(threads VS. posts)
You basically put tablename.column (column also known as row).
(2009-02-11, 05:04 AM)Rcpalace Wrote: [ -> ]You basically put tablename.column (column also known as row).
So if I do:
while($result = mysql_fetch_array($query))
I'd then do:
$threadName = $thread.title;
??
Rcpalace's example only shows how to compare data against another table, not actually include it in the results returned by the query/MySQL.

Here's a bit better example:

SELECT p.message, t.subject
FROM mybb_posts p
LEFT JOIN mybb_threads t ON(p.tid=t.tid)
WHERE uid='123'
LIMIT 0, 50

The first part, tells MySQL that we want the "message" and the "subject" in our result.

The second part tells MySQL that the main table we want to look at is our posts table and we give it an alias of "p".

The third part tells MySQL that we want to include our threads table with our posts table and assign rows based on the same tid (thread id)

The fourth part tells MySQL that we only want the posts from user '123' and the fourth part simply tells MySQL to only return the first 50 results.
Pages: 1 2