Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a two tables with following details

Table 1: tbl_comments
fields: comment_id (Primary Key), comment_text, thread_id, user_id (Foreign Key);

Table 2: tbl_users
fields: user_id (Primary Key), user_name, user_total_posts, user_total_threads;


Suppose There are 5 Comments of a thread (thread_id = 110) in tbl_comments and I want to Select all those comments using (SELECT * FROM tbl_comments WHERE thread_id=110) but the problem is, I also want to get the user_total_posts and user_total_threads data that is in tbl_users on the base of user_id in tbl_comments.

Which SQL Statements can solve my problem, please guide me.
Posted

Have you tried JOINING your tables?

Using Joins

Try something like this:

SQL
SELECT
    tbl_users.* --Choose desired fields
    ,tbl_comments.* --Choose desired fields
FROM
    tbl_users
    
    INNER JOIN tbl_comments ON
        tbl_comments.user_id = tbl_users.user_id
WHERE
    tbl_comments.thread_id = 110


Hope it helps
 
Share this answer
 
Comments
rashidfarooq 11-Feb-13 11:38am    
Thanks for the Solution That Really Worked Fine for me. My Problem solved...
Have you heard of JOINS ?
Try:

SQL
SELECT C.*, U.user_total_posts, U.user_total_threads
FROM tbl_comments AS C
LEFT OUTER JOIN tbl_users AS U
ON C.user_id = U.user_id
WHERE C.thread_id=110
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900