Jump to content

need help with Mysql queries.


stopblackholes

Recommended Posts

ok i have three tables

 

post table

-----------------

post_id --- category_id --- author_id -- content --- ect

 

 

categories table

-----------------

category_id --- category_name

 

 

users table

------

author_id --- username --- pw

 

 

whats the best way to query posts and fetch the related category_name and username with the post? how would i do it?

 

should i do three separate select queries? or is there a better way? performance wise

 

 

 

or say i have three table relationship. how would i query posts and show the related category with the post?

 

post table

----------------

post_id--- category_id ---content

 

 

category_post_rel table

------------------------

post_id  category_id

 

 

categories table

--------------------

category_id  category_name

 

 

I just started getting into mysql about a month ago i understand simple queries selects and joins. i know its better to normalize your database with many to many relationships but i just don't understand the complex queries. Id rather learn the right way now then go over the whole thing again in a few months.

 

Link to comment
Share on other sites

ok thanks i think i got one query working (only pulled one result so far)

 

 

 

does this look correct?

 

$query = "SELECT * FROM post LEFT JOIN categories ON
categories.category_id=post.category_id LEFT JOIN users ON users.author_id=post.author_id WHERE post.post_id='$post_id'";

 

 

i was curious what type of join works best. INNER JOIN vs LEFT JOIN, or is there another more efficient way?

Link to comment
Share on other sites

oops figured it out inner joins need

 

$query = "SELECT * FROM ((`post`
inner JOIN categories.category_id=post.category_id)
inner JOIN users ON users.author_id=post.author_id)"; 

 

 

you are missing the second table in you join - categories

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.