Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fIRKi-0005MA-Ud for pgsql-general@arkaria.postgresql.org; Tue, 15 May 2018 04:13:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fIRKf-0002rm-Om for pgsql-general@arkaria.postgresql.org; Tue, 15 May 2018 04:13:01 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fIRKe-0002rd-4G for pgsql-general@lists.postgresql.org; Tue, 15 May 2018 04:13:01 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fIRKa-00074X-83 for pgsql-general@lists.postgresql.org; Tue, 15 May 2018 04:12:58 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id BB3A822473; Tue, 15 May 2018 00:12:54 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Tue, 15 May 2018 00:12:54 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=jkI26QBruHNH8/+L3wcDmkqw0r0a0 FGv01TMpF+9+6I=; b=kin74r1UKrN69Yp3DjkiwElg/1mJ88bfqWx7L6tggLHbk wP3S5vNqV/1C6l6teGTaYAa1ytcXcuK0xkmQ4ia88BXSXtdNXa/56UAXFXQkQRbi qpNqEDyYbuQ0EwXeKD6UhwhhtCcKFmpWyM/i0/UDrsFWwX+w0uwXYDKac5s/xKZX 7bS7z6/CkEQLY1tbicQZW8tkxVOo2Yh9RSQT3gMV7VO3KqsRY3bYDJ8Ck46weh4o RosJJ+X42hjOjoAuZtKytoFw8M3znFOK4/RJpyNe68N1rn6JbA1tgSI8Jva5d+Lg FGssQF6D2KiraB4v6wwtkITNUi7i7xJktshvYR20Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=jkI26Q BruHNH8/+L3wcDmkqw0r0a0FGv01TMpF+9+6I=; b=EnoMGUtdMHe1UVm1qKIReR 0KvJOYW/Q2vOmZF+XLQyG7Mr4mejgSF+L3SSvn334alGW8snCOoiBnHdmf9d+atK h5G30d3WseSyijHq873Ky2MGFMskjgHAbXkT8IAM4sR2wNM/+TVI1+SgnjtscuWM mS0XSQ1rPLlYEZ7wNr9KD4xc5rKvelhyH2erybmkwHqNReazgxaaIFk4lbe6Hybc nUpovW03eyq38sbVNE//lOSh2FjxqdVvK5aNwitKD59yWroGcunOmVbcUiYhXBBn PpGnZY5Ju4qlZVdnivQPxWShiwLpnsSsowctOV4Kba70iCwVyImWWLQtoMIIkl8Q == X-ME-Sender: Received: from [192.168.1.2] (unknown [71.212.135.74]) by mail.messagingengine.com (Postfix) with ESMTPA id DDF4B10262; Tue, 15 May 2018 00:12:53 -0400 (EDT) Subject: Re: Query ID Values To: tango ward , melvin6925 , "pgsql-generallists.postgresql.org" References: <5afa3e76.1c69fb81.c680b.185d@mx.google.com> From: Adrian Klaver Message-ID: <927dc5f8-b343-c7ff-e131-17c5c3dc6814@aklaver.com> Date: Mon, 14 May 2018 21:12:53 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 05/14/2018 08:30 PM, tango ward wrote: > for row in cur_t: >         course = row['course'] >         cur_p.execute(""" >                           SELECT id >                           FROM education_program >                           WHERE name=%s >                           AND department_id >                           IN (SELECT id FROM profile_department WHERE > school_id=1) >                           """, (course,)) >         x = cur_p.fetchall() >         print x > > So far I can see the program IDs but I am still getting empty list. Also That would seem to indicate that the value of course is not matching any value in the field name for the given school_id. Maybe do: print(course) to see if they are valid values. > the program_id seems to be in a nested list. Why is that? Because you are doing fetchall(). That is going to fetch a list of row tuples. Either iterate over that list or iterate over the cursor: for row in cur_p: print(row) For more info see: http://initd.org/psycopg/docs/cursor.html > > On Tue, May 15, 2018 at 10:47 AM, tango ward > wrote: > > Hi, > > Yes sorry, here's the tables: > > [cur_t DB] [student_profile table] > > >         Column        |          Type          | Collation | > Nullable |        Default        | > ----------------------+------------------------+-----------+----------+-----------------------+ >  studentnumber        | character varying(45)  |           | not > null | ''::character varying | >  firstname            | character varying(60)  | > |          |                       | >  middlename           | character varying(60)  | > |          |                       | >  lastname             | character varying(60)  | > |          |                       | >  course               | character varying(150) |           | not > null | ''::character varying | > > > > [cur_p DB] [profile table] > >         Column        |          Type          | Collation | > Nullable |        Default        | > ----------------------+------------------------+-----------+----------+-----------------------+ >  studentnumber        | character varying(45)  |           | not > null | ''::character varying | >  firstname            | character varying(60)  | > |          |                       | >  middlename           | character varying(60)  | > |          |                       | >  lastname             | character varying(60)  | > |          |                       | >  program_id           | integer                |           | not > null |                       | >  department_id        | integer                |           | not > null |                       | >  campus_id            | integer                |           | not > null |                       | > > > > So I am migrating the data from one database to another. Here, I am > moving data of student from student_profile table to profile table. > > I have already migrated the course data to another table. What I > would like to do is get the value of program_id and department_id > for the profile table. I want to check if the course exist in > profile_program table, then get it's ID. I think I can use the same > logic for getting and setting value for the department_id column of > profile table. I am using psycopg2 to access and move the data. > > > for row in cur_t: >           course = row['course'] >           # Here I would like to get the value of program_id and > department_id and insert it to the said columns but I don't know how > to do it yet >           # I put ?? in department_id coz I don't know how to > access the 3 department IDs in this query. >           cur_p.execute(""" SELECT id from st_profile where > name='$[course]' and department_id=?? """) >           x = cur_p.fetchall() >           # This will print an error since I added department_id > without value yet but if I remove it, I will get "None" >           print x > > > > Sorry for asking questions a lot, we don't have DBA at the moment. > > > Thanks, > J > > > On Tue, May 15, 2018 at 9:57 AM, melvin6925 > wrote: > > Perhaps if you care to provide us with the structure of all > tables involved, we could suggest a reasonable query. > > > > Sent via the Samsung Galaxy S® 6, an AT&T 4G LTE smartphone > > -------- Original message -------- > From: tango ward > > Date: 5/14/18 21:08 (GMT-05:00) > To: "pgsql-generallists.postgresql.org > " > > > Subject: Query ID Values > > > Good Day, > > I need to run an SQL query and get a program_id and > department_id of a specific course for each student. I am > thinking of running an IF condition to check if the course name > is in program and get it's ID but I don't know yet where to use > the IF condition in the query. > > sample code: > > for row in cur_t: >      course = row['course'] > > >      cur_p.execute("""SELECT id from program where > name='$[course]'] >                                WHERE department_id=?? """) > > > Problem is I have 3 department IDs ( Senior High, Vocational, > Undergraduate ) and each ID have multiple programs/courses. Each > program/course is connected to the deparment table via > department_id. > > May I ask an advice on how to approach this? > > > Thanks, > J > > > -- Adrian Klaver adrian.klaver@aklaver.com