Mysql joins multiple tables.

Source: Internet
Author: User
I want to query the user table by username to obtain the user ID, query the comment table by user ID to obtain all comments from this user, query the topic table by the tid field in the comment, and obtain the post where the comment is located, obtain the author of the post by using the Post uid. The problem is that I am from the user table... I want to query the user table by username to obtain the user ID, query the comment table by user ID to obtain all comments from this user, query the topic table by the tid field in the comment, and obtain the post where the comment is located, obtain the author of the post by using the Post uid.

The problem is that I started to query the user table and joined the comment table and topic table respectively. when I checked the post author through the post uid, you also need to check the username from the user table. this time username refers to the username of the author who posted the post, not the username passed in at the beginning.

Mysql always prompts me Column 'username' in where clause is ambiguous

Why...

Reply content:

I want to query the user table by username to obtain the user ID, query the comment table by user ID to obtain all comments from this user, query the topic table by the tid field in the comment, and obtain the post where the comment is located, obtain the author of the post by using the Post uid.

The problem is that I started to query the user table and joined the comment table and topic table respectively. when I checked the post author through the post uid, you also need to check the username from the user table. this time username refers to the username of the author who posted the post, not the username passed in at the beginning.

Mysql always prompts me Column 'username' in where clause is ambiguous

Why...

When a table is connected, the query field must clearly specify the table field, for example
SELECT (a. username, a. id, B., C.) FROM user AS
LEFTJOIN comment AS B ON a. id = B. uid
LEFTJOIN topic AS c ON B. tid = c. id

Can I post the SQL statement? because you have queried the user table twice. if it is an SQL statement, you need to define an alias for the two user tables respectively, otherwise, the Column 'username' in where clause is ambiguous error will be reported if you directly use username in where. use the alias in the where condition. username method

For the select table as, the name is similar to: select * from tmp as t1 left join tmp as t2 on t1.uid = t2.uid

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.