Jump to content

INNER JOIN


bobicles2

Recommended Posts

Select * FROM "Patient"

INNER JOIN "GP"

ON Patient.GP_Name = GP.GP_Name

ORDER BY Patient.Name

 

 

My Tables are

 

Patient (

  "N.I.Number" character varying(100) NOT NULL,

  "Name" character varying(100) NOT NULL,

  "Address" character varying(200) NOT NULL,

  "GP_Name" character varying(100) NOT NULL,

 

GP

"ID" serial NOT NULL,

  "GP_Name" character varying(100),

  "Practice" character varying(100),

  CONSTRAINT "GP_pkey" PRIMARY KEY ("ID")

 

 

Want to create a query that produces an alphabetic list of all patients for a GP

 

 

been stuck for hours now

 

thanks

Link to comment
Share on other sites

If you want a list of patients for one specific GP:

 

Select * FROM "Patient"
WHERE GP_Name = 'Dr Btherl'
ORDER BY Name

 

If you want to do the same but selecting by the gp's id then you will need the join:

 

Select * FROM "Patient"
INNER JOIN "GP"
ON Patient.GP_Name = GP.GP_Name
WHERE GP.ID = 5
ORDER BY Patient.Name

 

I should mention it's a bit odd to be using GP_Name for joining when GP has an ID column.  Usually you would put the id column in the patient table and join on id.  The reason is that if a GP's name changes, you just need to update the GP table only.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.