Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL SELECT query for multiple tables
    primarykey
    data
    text
    <p>I have these tables:</p> <pre><code>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 </code></pre> <p>Now I used this situation:</p> <p>in USERS table I have: </p> <pre><code>(1, 'admin, '5hj63jhn120011', 'admin@gmail.com', '15.02.2012') </code></pre> <p>in POSTS table: </p> <pre><code>(1, 1, 'Hello', 'Content goes here', '15.02.2012') </code></pre> <p>in CATEGORY table: </p> <pre><code>(1, 1, 'web developing'), (2, 1, 'software programming'), (3, 1, 'web design') </code></pre> <p>So I have only 1 user, 1 post, and 3 categories. How to select them in one statement? I used this statement: </p> <pre><code>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 </code></pre> <p>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.</p> <p>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:</p> <pre><code>COMMENTS: comment_ID user_ID post_ID c_username c_email c_content </code></pre> <p>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) ? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload