-2

基本上,我有一个函数可以将数据导出为 csv 格式的 excel ......我试图有一个查询来选择所选时间段内的所有销售记录,但我需要查询到的第一列来自另一张桌子。我当前的查询如下:

$select = "SELECT `record_date` AS 'Sale Date',
                  `record_order_number` AS 'Order #',
                  `record_activity_number` AS 'Activity #',
                  `record_total_diy_revenue` AS 'Revenue $'
             FROM st_entered_records 
            WHERE `record_user_id` = '$this->user_id' AND 
                  `record_date` BETWEEN '$this->start_date' AND '$this->end_date' ";

`record_date` AS 'Sale Date'

我需要它运行第二次选择以获取用户的名字和姓氏格式:

"LastName, FirstName"
 (user_last_name)", "(user_first_name) from a second table (st_users) 
 where `user_id` = '$this->user_id'

任何帮助,将不胜感激!:)

4

2 回答 2

1

您可以做一个连接示例:

    SELECT t2.lastName,t2.firstName,
           t1.record_date AS 'Sale Date',
           t1.record_order_number AS 'Order #',
           t1.record_activity_numbe AS 'Activity #',
           t1.record_total_diy_revenue AS 'Revenue $' 

    FROM st_entered_records1 t1

    INNER JOIN stusers t2 ON t1.record_user_id=t2.userid

    WHERE t1.userid = $this->user_id AND
    t1.record_date BETWEEN $this->start_date AND $this->end_date

确保你匹配表名和字段名我相信你已经知道了:)

编辑: Stongly advise you do not use MYSQL_*

原因:

This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

于 2013-01-20T06:05:30.100 回答
0

尝试

SELECT
    `st_entered_records`.`record_date` AS 'Sale Date',
    `st_entered_records`.`record_order_number` AS 'Order #',
    `st_entered_records`.`record_activity_number` AS 'Activity #',
    `st_entered_records`.`record_total_diy_revenue` AS 'Revenue $',
    `st_users`.`user_last_name` AS 'LastName',
    `st_users`.`user_first_name` AS 'FirstName',
FROM `st_entered_records`
INNER JOIN `st_users` ON `st_entered_records`.`record_user_id` = `st_users`.`user_id`
WHERE
    `record_user_id` = :uid AND
    `record_date` BETWEEN :startDate AND :endDate;

顺便说一句,我已经用命名参数替换了变量。我强烈推荐使用准备好的语句,但如果你坚持使用连接查询(无论是 mysql、mysqli 还是 PDO),请记住对变量进行转义!

于 2013-01-20T06:08:09.067 回答