#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    PostgreSQL Query


    Hi All,

    Sorry, i am just a beginner and looking to tune up my query.

    Select student_id,net_id from nstudent where status_id=3 inner join ndepart using (net_id) where not deleted;

    The above query shows the result but i also want to get the depart_name which is ndepart table, but i cannot figure out how would i do it (probably in descending order)?

    student_id,net_id are bigint. depart_name is character, and status_id is integer. Also net_id is primary key for ndepart and foreign key in nstudent and student_id is primary key for nstudent.

    Thank you
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    158
    Rep Power
    10
    You can always reference any column in any of the joined tables by prefixing the tablename using a dot:

    SELECT foo.x, foo.this, bar.that
    FROM foo
    INNER JOIN bar ON foo.x = bar.y;

    If the fieldname is unique then you don't have to refix, but it is good practice to always do it anyway. Otherwise, crappy databases like MySQL will not warn you that, for example, both tables have a column named "id", and you will never know which you got in your result.

IMN logo majestic logo threadwatch logo seochat tools logo