Question about joining multiple tables in mysql.

WBOY
Release: 2016-07-06 13:53:17
Original
1120 people have browsed it

I want to query the user table through the username to get the user ID, query the comment table through the user ID to get all the comments of this user, query the topic table through the tid field in the comment, and get the post where the comment is located, and pass the post's uid gets the author of this post.

Here comes the problem. I start the query from the user table, and join the comment table and topic table respectively. Then when I check the post author through the post uid, I need to check the username from the user table again. This time the username refers to is the username of the author who published the post, not the username passed in at the beginning.

mysql keeps prompting me Column 'username' in where clause is ambiguous

Why...

Reply content:

I want to query the user table through the username to get the user ID, query the comment table through the user ID to get all the comments of this user, query the topic table through the tid field in the comment, and get the post where the comment is located, and pass the post's uid gets the author of this post.

Here comes the problem. I start the query from the user table, and join the comment table and topic table respectively. Then when I check the post author through the post uid, I need to check the username from the user table again. This time the username refers to is the username of the author who published the post, not the username passed in at the beginning.

mysql keeps prompting me Column 'username' in where clause is ambiguous

Why...

When querying fields when connecting tables, you must clearly indicate which table they belong to, such as
SELECT(a.username,a.id,b.,c.) FROM user AS a
LEFTJOIN comment AS b ON a.id = b.uid
LEFTJOIN topic AS c ON b.tid = c.id

Can you post the sql? Because you have queried the user table twice. If it is a sql, you have to define aliases for the two user tables respectively. Otherwise, if you use username directly in where, it will report Column ' username' in where clause is ambiguous error. Please use alias.username in the where condition

<code>给select 的表 as 一个名称
类似:select * from tmp as t1 left join tmp as t2 on t1.uid=t2.uid</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template