Jump to content

Inner Join 2 tables, and fetch results during while


Russia

Recommended Posts

I made this query so it combines 2 tables into one so It can get the name of the office and insurance company instead of the number variable it is but its not working.
 

$values = mysql_query("SELECT pat_id,pat_lname,pat_fname,pat_date,pat_loc,pat_ins,pat_show FROM patients INNER JOIN offices ON office_id = pat_loc INNER JOIN insurance ON ins_id = pat_ins ");

How the offices database looks like:
dsRCYEn.png
 
How it looks when exporting:
4zahBq3.png
 
I am trying to get it to show the name Hackensack instead of a 2 for the location in pat_loc when it exports. How can I get that to work?

Here is the whole statement:
 

$values = mysql_query("SELECT pat_id,pat_lname,pat_fname,pat_date,pat_loc,pat_ins,pat_show FROM patients INNER JOIN offices ON office_id = pat_loc INNER JOIN insurance ON ins_id = pat_ins ");


while ($rowr = mysql_fetch_row($values)) {
    for ($j = 0; $j < $i; $j++) {
        $csv_output .= $rowr[$j] . ", ";
    }
    $csv_output .= "\n";
} 
Edited by Russia
Link to comment
Share on other sites

I've only just started playing with join statements, but maybe you could give this a try:

 

 

SELECT pat_id,pat_lname,pat_fname,pat_date,pat_loc,pat_show, (SELECT office_name FROM offices WHERE offices.office_id = patients.pat_ins) AS name_office FROM patients

 

I don't have your database code, so I can't really test that, but the idea is that the SELECT within the parenthesis will grab the office_name from the table offices where office_id in offices matches pat_ins from patients (I think I got the fields right for what you were after?), and put it into the results as name_office.

 

But like I said, I could be way off base here.

 

Hopefully that'll give you something else to look into.

 

Denno

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.