I have these tables:
USERS:
user_ID
username
password
email
data_registered
POSTS:
post_ID
user_ID
post_title
post_content
post_data
CATEGORY:
cat_ID
post_ID
cat_title
Now I used this situation:
in USERS table I have:
(1, 'admin, '5hj63jhn120011', 'admin@gmail.com', '15.02.2012')
in POSTS table:
(1, 1, 'Hello', 'Content goes here', '15.02.2012')
in CATEGORY table:
(1, 1, 'web developing'),
(2, 1, 'software programming'),
(3, 1, 'web design')
So I have only 1 user, 1 post, and 3 categories. How to select them in one statement? I used this statement:
SELECT
p.post_title,
p.post_content,
u.username,
c.cat_title
FROM
posts p
LEFT JOIN users u
ON p.user_ID = u.user_ID
LEFT JOIN category c
ON c.post_ID = p.post_ID
but this select 3 rows with 3 different categories. I’m still learning to make this JOIN queries properly so I would appreciate any help.
And after that, for example, I will have situation to select data from COMMENTS table as well, so this is going to be complicated. Just to mention this is comment table:
COMMENTS:
comment_ID
user_ID
post_ID
c_username
c_email
c_content
It’s gonna be tough to select this comments with user , post and category altogether. So one more question would be can I split this select queries and is this a good practice generaly (when developing with PHP) ?
Try:
– if you want to see all the categories for a single post in the same row.