jillybean
02-15-2009, 05:30 AM
Hi all,
I'm very new to MySQL and am having some trouble figuring out a few things, specifically joining tables. I understand that aliases are needed to use the function properly but for some reason I can't crack this code. Many of the things I'm trying to do are similar to math word problems, which I get severe anxiety over :confused: so it's as if some mental block is put up.
What I'm trying to do is come up with commands to use on a database I have created for a small hospital. I'm GUESSING that I need to join a table to itself but not 100% positive. I listed below the tables that I have in my data base just in case.
List all patients with the same insurer but different doctors.
List all patients with the same last name and same doctor.
All of this information can be found in one table called "Visit." The entries needed are labeled as:
FIRST_NAME (this is patient's first name)
LAST_NAME (this is patient's last name)
DR_NAME (this is patient's doctor name)
INS_NAME (this is patient's insurer name)
This is what I have so far but I know it's wrong:
SELECT F.INS_NAME, F.LAST_NAME, F.DR_NAME, S.INS_NAME, S.LAST_NAME, S.DR_NAME
FROM VISIT F, VISIT S
WHERE F.LAST_NAME = S.LAST_NAME
AND F.DR_NAME < S.DR_NAME
ORDER BY F.LAST_NAME, S.LAST_NAME
Can someone please help me? I know these have GOT to be so simple but for the life of me, I cannot get it to work.
Thank you in advance!
Jill
ETA: I may be wrong in thinking that I need to join a table to itself for this particular case. I'm going to list the tables that I have in my database to see if I could get this another way.
Insurer Table
INS_NUM (primary key)
INS_NAME
STREET
CITY
STATE
ZIP
PHONE
Address table
FIRST_NAME
LAST_NAME
PATIENT_NUM (primary key)
ADDRESS
CITY
STATE
ZIP
PHONE
Patient table
FIRST_NAME
LAST_NAME
PATIENT_NUM (primary key)
DOB
DR_NAME
INS_NUM
BALANCE_DUE
Visit table
FIRST_NAME
LAST_NAME
PATIENT_NUM (primary key)
DATE
DR_NAME
COMPLAINT
CHARGE
AMOUNT_PD
INS_NAME
INS_NUM
I'm very new to MySQL and am having some trouble figuring out a few things, specifically joining tables. I understand that aliases are needed to use the function properly but for some reason I can't crack this code. Many of the things I'm trying to do are similar to math word problems, which I get severe anxiety over :confused: so it's as if some mental block is put up.
What I'm trying to do is come up with commands to use on a database I have created for a small hospital. I'm GUESSING that I need to join a table to itself but not 100% positive. I listed below the tables that I have in my data base just in case.
List all patients with the same insurer but different doctors.
List all patients with the same last name and same doctor.
All of this information can be found in one table called "Visit." The entries needed are labeled as:
FIRST_NAME (this is patient's first name)
LAST_NAME (this is patient's last name)
DR_NAME (this is patient's doctor name)
INS_NAME (this is patient's insurer name)
This is what I have so far but I know it's wrong:
SELECT F.INS_NAME, F.LAST_NAME, F.DR_NAME, S.INS_NAME, S.LAST_NAME, S.DR_NAME
FROM VISIT F, VISIT S
WHERE F.LAST_NAME = S.LAST_NAME
AND F.DR_NAME < S.DR_NAME
ORDER BY F.LAST_NAME, S.LAST_NAME
Can someone please help me? I know these have GOT to be so simple but for the life of me, I cannot get it to work.
Thank you in advance!
Jill
ETA: I may be wrong in thinking that I need to join a table to itself for this particular case. I'm going to list the tables that I have in my database to see if I could get this another way.
Insurer Table
INS_NUM (primary key)
INS_NAME
STREET
CITY
STATE
ZIP
PHONE
Address table
FIRST_NAME
LAST_NAME
PATIENT_NUM (primary key)
ADDRESS
CITY
STATE
ZIP
PHONE
Patient table
FIRST_NAME
LAST_NAME
PATIENT_NUM (primary key)
DOB
DR_NAME
INS_NUM
BALANCE_DUE
Visit table
FIRST_NAME
LAST_NAME
PATIENT_NUM (primary key)
DATE
DR_NAME
COMPLAINT
CHARGE
AMOUNT_PD
INS_NAME
INS_NUM