Ask two Table association queries

Source: Internet
Author: User
Function Details ($id) {
$query = $this->db->query ("Select T1.gradename, t2.* from grade as t1,student as t2 where StudentID = ' $id ' and T2.G Radeid = T1.gradeid ");
return $query->result ();
}

There are two tables, student tables and grade tables, the call method passed over the ID variable used to query the student information for the specified number, and now ask, I want to query all the field values of Sutdent and gradename values.
Because student and grade are foreign key associations student the Gradeid and grade tables of the table Gradeid, Gradename exists grade table.

I write the statement is wrong, can not execute, please big guy to help, thank you.


Reply to discussion (solution)

What is the error message? SQL does not see the problem ...

Select S.*, g.gradename from student as s left joins grade as G on s.gradeid = G.gradeid where S.studentid = $id




What is the error message? SQL does not see the problem ...



#1052-column ' StudentID ' in WHERE clause is ambiguous

Wander_wind wrote it right, but I don't understand why I was wrong.

Column ' StudentID ' in WHERE clause is ambiguous
The StudentID column in the WHERE clause is ambiguous.

In a multi-table query, it is necessary for the column name to be prefixed with it, otherwise MySQL is not clearly divided.

Select T1.gradename, t2.* from grade as t1,student as t2 where T1.studentid = ' $id ' and T2.gradeid = T1.gradeid

Wander_wind wrote it right, but I don't understand why I was wrong.


The most common reason for this error is that the same field in the associated table is causing the prefix of the table alias as it was said upstairs.
  • 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.