我的第一张表list_info
list_id | list_name | list_link | list_extrafee | list_reviews
------------------------------------------------------------------------
4852 | List One | /list1 | $4 after 3 guests | 56
4853 | List Two | /list2 | $10 after 5 guests | 42
4854 | List Three | /list3 | $40 after 8 guests | 32
4855 | List Four | /list4 | FREE | 14
第二张表list_price
list_id | dates | list_price | list_avilable
------------------------------------------------------
4852 | 4/12/2015 | 55 | TRUE
4852 | 5/12/2015 | 56 | TRUE
4852 | 6/12/2015 | 89 | TRUE
4852 | 7/12/2015 | 110 | TRUE
4852 | 8/12/2015 | 150 | TRUE
4852 | 9/12/2015 | 110 | TRUE
4852 | 10/12/2015 | 110 | TRUE
4852 | 11/12/2015 | 150 | TRUE
4852 | 12/12/2015 | 110 | TRUE
4853 | 4/12/2015 | 160 | TRUE
4853 | 5/12/2015 | 140 | FALSE
4853 | 6/12/2015 | 55 | FALSE
4853 | 7/12/2015 | 56 | FALSE
4853 | 8/12/2015 | 89 | FALSE
4853 | 9/12/2015 | 110 | FALSE
4853 | 10/12/2015 | 150 | FALSE
4853 | 11/12/2015 | 110 | FALSE
4853 | 12/12/2015 | 110 | FALSE
4854 | 4/12/2015 | 150 | TRUE
4854 | 5/12/2015 | 110 | TRUE
4854 | 6/12/2015 | 160 | TRUE
4854 | 7/12/2015 | 140 | TRUE
4854 | 8/12/2015 | 160 | TRUE
4854 | 9/12/2015 | 180 | TRUE
4854 | 10/12/2015 | 110 | TRUE
4854 | 11/12/2015 | 110 | TRUE
4854 | 12/12/2015 | 50 | TRUE
4855 | 4/12/2015 | 50 | TRUE
4855 | 5/12/2015 | 48 | TRUE
4855 | 6/12/2015 | 60 | TRUE
4855 | 7/12/2015 | 70 | TRUE
4855 | 8/12/2015 | 65 | TRUE
4855 | 9/12/2015 | 90 | TRUE
4855 | 10/12/2015 | 80 | TRUE
4855 | 11/12/2015 | 70 | TRUE
4855 | 12/12/2015 | 60 | TRUE
我想要实现的目标是:
到目前为止,我设法只将其中一个表放入一个数组并使用$objPHPExcel->getActiveSheet()->fromArray();
函数传递它,代码:
public function scrape()
{
/** Error reporting */
error_reporting(E_ALL);
/** Include path **/
ini_set('include_path', ini_get('include_path').';../Classes/');
/** PHPExcel */
include 'PHPExcel.php';
/** PHPExcel_Writer_Excel2007 */
include 'PHPExcel/Writer/Excel2007.php';// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set properties
$objPHPExcel->getProperties()->setCreator("AB")
->setLastModifiedBy("AB")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("AB document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("AB result file");
// Add some data
$q = $this->db->select('*')->from('list_info')->get();
$sheet=$q->result_array();
$objPHPExcel->getActiveSheet()->fromArray($sheet);
// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('AB Result '.date('d-m-Y').'');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a clients web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
我需要帮助将这两个表组合成一个数组,该数组适用于生成我上传的示例excel文件..
<2> Somnath Muluk的回答Error
SQL查询:
PREPARE stmt FROM @sql;
MySQL说:文档
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `list_info` l JOIN list_price p ON l.list_id = p.list_id
' at line 15
长期工作代码:
SELECT l.*,
MAX( CASE
WHEN p.list_date = "2015-11-29" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-11-29" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-11-29`,
MAX( CASE
WHEN p.list_date = "2015-11-30" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-11-30" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-11-30`,
MAX( CASE
WHEN p.list_date = "2015-12-01" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-01" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-01`,
MAX( CASE
WHEN p.list_date = "2015-12-02" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-02" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-02`,
MAX( CASE
WHEN p.list_date = "2015-12-03" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-03" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-03`,
MAX( CASE
WHEN p.list_date = "2015-12-04" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-04" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-04`,
MAX( CASE
WHEN p.list_date = "2015-12-05" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-05" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-05`,
MAX( CASE
WHEN p.list_date = "2015-12-06" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-06" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-06`,
MAX( CASE
WHEN p.list_date = "2015-12-07" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-07" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-07`,
MAX( CASE
WHEN p.list_date = "2015-12-08" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-08" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-08`,
MAX( CASE
WHEN p.list_date = "2015-12-09" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-09" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-09`,
MAX( CASE
WHEN p.list_date = "2015-12-10" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-10" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-10`,
MAX( CASE
WHEN p.list_date = "2015-12-11" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-11" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-11`,
MAX( CASE
WHEN p.list_date = "2015-12-12" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-12" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-12`,
MAX( CASE
WHEN p.list_date = "2015-12-13" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-13" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-13`,
MAX( CASE
WHEN p.list_date = "2015-12-14" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-14" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-14`,
MAX( CASE
WHEN p.list_date = "2015-12-15" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-15" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-15`,
MAX( CASE
WHEN p.list_date = "2015-12-16" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-16" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-16`,
MAX( CASE
WHEN p.list_date = "2015-12-17" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-17" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-17`,
MAX( CASE
WHEN p.list_date = "2015-12-18" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-18" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-18`,
MAX( CASE
WHEN p.list_date = "2015-12-19" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-19" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-19`,
MAX( CASE
WHEN p.list_date = "2015-12-20" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-20" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-20`,
MAX( CASE
WHEN p.list_date = "2015-12-21" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-21" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-21`,
MAX( CASE
WHEN p.list_date = "2015-12-22" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-22" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-22`,
MAX( CASE
WHEN p.list_date = "2015-12-23" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-23" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-23`,
MAX( CASE
WHEN p.list_date = "2015-12-24" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-24" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-24`,
MAX( CASE
WHEN p.list_date = "2015-12-25" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-25" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-25`,
MAX( CASE
WHEN p.list_date = "2015-12-26" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-26" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-26`,
MAX( CASE
WHEN p.list_date = "2015-12-27" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-27" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-27`,
MAX( CASE
WHEN p.list_date = "2015-12-28" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-28" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-28`,
MAX( CASE
WHEN p.list_date = "2015-12-29" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-29" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-29`,
MAX( CASE
WHEN p.list_date = "2015-12-30" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-30" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-30`,
MAX( CASE
WHEN p.list_date = "2015-12-31" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2015-12-31" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2015-12-31`,
MAX( CASE
WHEN p.list_date = "2016-01-01" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-01-01" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-01-01`,
MAX( CASE
WHEN p.list_date = "2016-01-02" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-01-02" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-01-02`,
MAX( CASE
WHEN p.list_date = "2016-01-03" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-01-03" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-01-03`,
MAX( CASE
WHEN p.list_date = "2016-01-04" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-01-04" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-01-04`,
MAX( CASE
WHEN p.list_date = "2016-01-05" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-01-05" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-01-05`,
MAX( CASE
WHEN p.list_date = "2016-01-06" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-01-06" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-01-06`,
MAX( CASE
WHEN p.list_date = "2016-01-07" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-01-07" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-01-07`,
MAX( CASE
WHEN p.list_date = "2016-01-08" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-01-08" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-01-08`,
MAX( CASE
WHEN p.list_date = "2016-01-09" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-01-09" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-01-09`,
MAX( CASE
WHEN p.list_date = "2016-01-10" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-01-10" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-01-10`,
MAX( CASE
WHEN p.list_date = "2016-01-11" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-01-11" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-01-11`,
MAX( CASE
WHEN p.list_date = "2016-01-12" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-01-12" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-01-12`,
MAX( CASE
WHEN p.list_date = "2016-01-13" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-01-13" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-01-13`,
MAX( CASE
WHEN p.list_date = "2016-01-14" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-01-14" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-01-14`,
MAX( CASE
WHEN p.list_date = "2016-01-15" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-01-15" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-01-15`,
MAX( CASE
WHEN p.list_date = "2016-01-16" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-01-16" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-01-16`,
MAX( CASE
WHEN p.list_date = "2016-01-17" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-01-17" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-01-17`,
MAX( CASE
WHEN p.list_date = "2016-01-18" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-01-18" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-01-18`,
MAX( CASE
WHEN p.list_date = "2016-01-19" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-01-19" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-01-19`,
MAX( CASE
WHEN p.list_date = "2016-01-20" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-01-20" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-01-20`,
MAX( CASE
WHEN p.list_date = "2016-01-21" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-01-21" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-01-21`,
MAX( CASE
WHEN p.list_date = "2016-01-22" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-01-22" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-01-22`,
MAX( CASE
WHEN p.list_date = "2016-01-23" and p.list_available = "TRUE" THEN p.list_price
ELSE NULL END) `2016-05-21`,
MAX( CASE
WHEN p.list_date = "2016-05-22" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-05-22" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-05-22`,
MAX( CASE
WHEN p.list_date = "2016-05-23" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-05-23" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-05-23`,
MAX( CASE
WHEN p.list_date = "2016-05-24" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-05-24" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-05-24`,
MAX( CASE
WHEN p.list_date = "2016-05-25" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-05-25" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-05-25`,
MAX( CASE
WHEN p.list_date = "2016-05-26" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-05-26" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-05-26`,
MAX( CASE
WHEN p.list_date = "2016-05-27" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-05-27" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-05-27`,
MAX( CASE
WHEN p.list_date = "2016-05-28" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-05-28" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-05-28`,
MAX( CASE
WHEN p.list_date = "2016-05-29" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-05-29" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-05-29`,
MAX( CASE
WHEN p.list_date = "2016-05-30" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-05-30" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-05-30`,
MAX( CASE
WHEN p.list_date = "2016-05-31" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-05-31" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-05-31`,
MAX( CASE
WHEN p.list_date = "2016-06-01" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-06-01" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-06-01`,
MAX( CASE
WHEN p.list_date = "2016-06-02" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-06-02" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-06-02`,
MAX( CASE
WHEN p.list_date = "2016-06-03" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-06-03" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-06-03`,
MAX( CASE
WHEN p.list_date = "2016-06-04" and p.list_available = "TRUE" THEN p.list_price
WHEN p.list_date = "2016-06-04" and p.list_available = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `2016-06-04`
FROM `airbnb_lists` l JOIN airbnb_lists_price p ON l.list_id = p.list_id
GROUP BY l.list_id
答案 0 :(得分:4)
我为您创建了demo database&amp;还在表格中只添加了一些虚拟数据。
您需要找出list_price中提供的不同日期。
如果您想要list_price中的动态列,则可以运行以下查询:
SELECT GROUP_CONCAT(DISTINCT
CONCAT('MAX( CASE
WHEN p.dates = "', dates,'" and p.list_avilable = "TRUE" THEN p.list_price
WHEN p.dates = "', dates,'" and p.list_avilable = "FALSE" THEN CONCAT( p.list_price , " FALSE")
ELSE NULL END) as `', dates,'`
' )
)
FROM list_price
这将为您提供动态列:
MAX( CASE
WHEN p.dates = "4/12/2015" and p.list_avilable = "TRUE" THEN p.list_price
WHEN p.dates = "4/12/2015" and p.list_avilable = "FALSE" THEN CONCAT( p.list_price , " FALSE")
ELSE NULL END) as `4/12/2015`,
MAX( CASE
WHEN p.dates = "5/12/2015" and p.list_avilable = "TRUE" THEN p.list_price
WHEN p.dates = "5/12/2015" and p.list_avilable = "FALSE" THEN CONCAT( p.list_price , " FALSE")
ELSE NULL END) as `5/12/2015`
需要进行如下查询。
// $columns will have result of first query.
$query= "SELECT l.*,
".$columns."
FROM `list_info` l JOIN list_price p ON l.list_id = p.list_id
GROUP BY l.list_id";
你的查询就像。
SELECT l.*,
MAX( CASE
WHEN p.dates = "4/12/2015" and p.list_avilable = "TRUE" THEN p.list_price
WHEN p.dates = "4/12/2015" and p.list_avilable = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `4/12/2015`,
MAX( CASE
WHEN p.dates = "5/12/2015" and p.list_avilable = "TRUE" THEN p.list_price
WHEN p.dates = "5/12/2015" and p.list_avilable = "FALSE" THEN CONCAT( p.list_price , ' FALSE')
ELSE NULL END) `5/12/2015`
FROM `list_info` l JOIN list_price p ON l.list_id = p.list_id
GROUP BY l.list_id
我能找到如下结果:
或您可以将两个sql查询合并为一个。您只需要使用以下原始查询语句替换$q = $this->db->select('*')->from('list_info')->get();
。
SET @sql= '';
SELECT GROUP_CONCAT(DISTINCT
CONCAT('MAX( CASE
WHEN p.dates = "', dates,'" and p.list_avilable = "TRUE" THEN p.list_price
WHEN p.dates = "', dates,'" and p.list_avilable = "FALSE" THEN CONCAT( p.list_price , " FALSE")
ELSE NULL END) as `', dates,'`
' )
) INTO @sql
FROM list_price;
SET @sql = CONCAT('SELECT l.*, ', @sql, '
FROM `list_info` l JOIN list_price p ON l.list_id = p.list_id
GROUP BY l.list_id');
PREPARE stmt FROM @sql;
EXECUTE stmt;
PHPMyadmin中此查询的一些问题。只显示行数。检查你是否得到PHP的结果。