哪个SQL语句可以解决我的问题 [英] Which SQL Statement Can Solve my Problem
问题描述
我有两张桌子,内有以下详细信息
表1: tbl_comments
字段: comment_id(主键),comment_text,thread_id,user_id(外键);
表2: tbl_users
字段: user_id(主键),user_name,user_total_posts,user_total_threads;
假设在tbl_comments中有5个线程的注释(thread_id = 110),我想使用(SELECT * FROM tbl_comments WHERE thread_id = 110)选择所有这些注释,但问题是,我还想获取user_total_posts和user_total_threads数据在tbl_comments中基于user_id的tbl_users中。
哪些SQL语句可以解决我的问题,请指导我。
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.
推荐答案
你有没有试过加入你的桌子?
使用连接
尝试这样的事情:
Have you tried JOINING your tables?
Using Joins
Try something like this:
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
你听说过吗? JOINS?
尝试:
Have you heard of JOINS ?
Try:
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
这篇关于哪个SQL语句可以解决我的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!