Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 左联接后从表中获取列_Mysql - Fatal编程技术网

Mysql 左联接后从表中获取列

Mysql 左联接后从表中获取列,mysql,Mysql,我必须编辑此查询: $q="select *, (select count(participant_id) from event_tables_seats left join participants on participants.id=event_tables_seats.participant_id where table_id=event_tables.id and participan

我必须编辑此查询:

$q="select *, (select count(participant_id) 
                from event_tables_seats 
                left join participants on participants.id=event_tables_seats.participant_id 
                where table_id=event_tables.id and participants.status='Active') as participants_in_tables 
    from event_tables 
    where event_id=?";
我的目标是从参与者表中选择列,例如participants.first\u name和participants.last\u name。这是我写的excel导出函数:

    public function export($event_id) 
    {   
    $data = $this->Jot_model->getTablesForEvent($event_id);


    $filename = "table_data_$event_id.xls";

    header("Content-Disposition: attachment; filename=\"$filename\"");
    header("Content-Type: application/vnd.ms-excel");

      $flag = false;
      foreach($data as $row) 
      {
        if (!$flag) 
        {
          echo implode("\t", array_keys($row)) . "\r\n";
          $flag = true;
        }
        echo implode("\t", array_values($row)) . "\r\n";
      }
      exit;
      } 

使用联接而不是相关子查询

SELECT e.*, p.first_name, p.last_name
FROM event_tables AS e
LEFT JOIN event_tables_seats AS s ON e.id = s.table_id
LEFT JOIN participants AS p ON p.id = s.participant_id
WHERE e.event_id = ?