0

我进行了此查询,因此它将 2 个表合并为一个,因此它可以获得办公室和保险公司的名称,而不是它的数字变量,但它不起作用。

$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 ");

office 数据库的外观如下:

http://i.stack.imgur.com/rXWYG.png

导出时的外观

http://i.stack.imgur.com/ienh4.png

我试图让它在导出时显示名称 Hackensack 而不是 2 用于 pat_loc 中的位置。我怎样才能让它工作?

这是整个声明:

$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";
} 
4

1 回答 1

1
SELECT
  pat_id,
  pat_lname,
  pat_fname,
  pat_date,
  office_name,
  pat_ins,
  pat_show
FROM patients
INNER JOIN offices ON office_id = pat_loc
INNER JOIN insurance ON ins_id = pat_ins

更新:根据您在下面的评论,我想出了以下内容:

$result = mysql_query('
    SELECT
        pat_id AS `user_id`,
        pat_fname as `First Name`,
        pat_lname as `Last Name`,
        pat_date as `IME Date`,
        pat_loc as `Location`,
        pat_ins as `Insurance Company`,
        pat_show as `Show(Y/N)`
    FROM patients
    INNER JOIN offices ON office_id = pat_loc
    INNER JOIN insurance ON ins_id = pat_ins
');

$csv_output = '';
$headersPrinted = false;
while ($patient = mysql_fetch_assoc($result))
{
    if (!$headersPrinted)
    {
        $csv_output .= implode(',', array_keys($patient))."\n";
        $headersPrinted = true;
    }

    $csv_output .= implode(',', array_values($patient))."\n";
}

但是,有了这个,您加入officesinsurance表没有明显的用途。我假设您想显示这些表中的一些值来代替原始 ID。在这种情况下,例如,您可以在查询中pat_loc替换为。office_name

于 2013-07-01T20:35:03.967 回答