简体   繁体   中英

SQL SELECT query for multiple tables

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:

SELECT p.post_title, 
       p.post_content, 
       u.username, 
       group_concat(c.cat_title) categories
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
group by p.post_id

- if you want to see all the categories for a single post in the same row.

Use something like that:

SELECT 
  p.post_title, 
  p.post_content, 
  u.username, 
  GROUP_CONCAT(c.cat_title + ',') cat_titles
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
GROUP BY 
  p.post_title, 
  p.post_content, 
  u.username

I'm not sure in proper usage of GROUP_CONCAT, but looks like if will work

I think it should be like this instead:

POSTS:
post_ID
user_ID
cat_ID
post_title
post_content
post_data

CATEGORY:
cat_ID
cat_title

As categories don't need posts to relate to but each post will need a category to relate to.

Update:

If many-to-many is the intention then it should be like this:

POSTS:
post_ID
user_ID
post_title
post_content
post_data

CATEGORY:
cat_ID
cat_title

POST_CATEGORY:
post_ID
cat_ID

without have tried it, I think the solution is:

SELECT p.post_title, p.post_content, u.username, c.cat_title 
FROM users u LEFT JOIN posts p ON u.user_ID = p.user_ID
LEFT JOIN category c ON p.post_ID = c.post_ID

if you change the order of the ID columns, the meaning of LEFT JOIN changes. So you get three result rows....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM