在mysql中使用join和group by子句

时间:2015-10-12 11:17:44

标签: php mysql

我想在查询中使用group by

 $sql = "select count(T1.coupon_id) as current_user,T1.coupon_id,T1.shadyab_profit,T1.seri
         from (select tractions.*,traction_details.* from traction_details,tractions
            where tractions.id = traction_details.tr_id
                AND (traction_details.sell_start_date >= '2015-09-10 01:00:00')
                AND confirm=2 AND traction_details.adviser = 7) T1
         group by T1.coupon_id,T1.seri,T1.shadyab_profit
        ";

  $result = mysql_query($sql) ;
  var_dump($result);

但它会返回false

我哪里错了?

更新

> 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 'current_user,T1.coupon_id,T1.shadyab_profit,T1.seri from (SELECT
> tr' at line 1



 $sql = "select count(T1.coupon_id) as current_user,T1.coupon_id,T1.shadyab_profit,T1.seri
         from (SELECT tractions.*,traction_details.* 
           FROM traction_details
           INNER JOIN tractions ON tractions.id = traction_details.tr_id
           WHERE (traction_details.sell_start_date >= '2015-09-10 01:00:00')
           AND [table_name_it_belongs].confirm=2 
           AND traction_details.adviser = 7) T1
           group by T1.coupon_id,T1.seri,T1.shadyab_profit
        ";

  $result = mysql_query($sql) ;
echo mysql_error();

牵引表:

enter image description here

traction_details表:

enter image description here

@updated:

SELECT COUNT(coupon_id), coupon_id, shadyab_profit, seri
FROM traction_details td INNER JOIN
     tractions t
     ON t.id = td.tr_id
WHERE (td.sell_start_date >= '2015-09-10 01:00:00') AND
       td.adviser = 7
GROUP BY coupon_id, seri, shadyab_profit, seri
            ";

      $result = mysql_query($sql) ;
 echo mysql_error();
var_dump($result);
       echo "=>>".count($result);

我收到了这个错误:

Column 'seri' in field list is ambiguous

解决:

     $query = $this->db->query("select count(T1.coupon_id) as `current_user`,T1.coupon_id,T1.shadyab_profit,T1.seri
      from (SELECT tractions.coupon_id,traction_details.*
        FROM traction_details
          INNER JOIN tractions ON tractions.id = traction_details.tr_id
          WHERE (traction_details.sell_end_date BETWEEN '$start_sell' AND '$end_sell')
          AND traction_details.adviser = 7 AND  tractions.succ=1 AND tractions.expired !=$adviser_id) T1
         group by T1.coupon_id,T1.seri,T1.shadyab_profit
        ");

4 个答案:

答案 0 :(得分:1)

子查询应该是JOIN -

SELECT tractions.*,traction_details.* 
FROM traction_details
INNER JOIN tractions ON tractions.id = traction_details.tr_id
WHERE (traction_details.sell_start_date >= '2015-09-10 01:00:00')
AND traction_details.confirm=2 
AND traction_details.adviser = 7

<强>更新

current_user是一个功能。你不能这样用。您可以使用反引号或更改名称。

SELECT COUNT(tractions.coupon_id) as `current_user`,tractions.coupon_id,traction_details.shadyab_profit,traction_details.seri 
FROM traction_details
INNER JOIN tractions ON tractions.id = traction_details.tr_id
WHERE (traction_details.sell_start_date >= '2015-09-10 01:00:00')
AND traction_details.confirm=2 
AND traction_details.adviser = 7
group by tractions.coupon_id,traction_details.seri,traction_details.shadyab_profit

答案 1 :(得分:0)

尽量避免使用名称&#39; current_user&#39;。 current_user是Mysql的默认函数。因此,尝试使用其他名称,例如&#39; currentuser&#39;或者&#39; current_users&#39;。

答案 2 :(得分:0)

您的问题可能是子查询中的命名冲突。虽然没有样本数据,但它几乎可以是任何东西 - 想到糟糕的表名和糟糕的列名。

首先摆脱子查询:

SELECT COUNT(coupon_id), coupon_id, shadyab_profi, seri
FROM traction_details td INNER JOIN
     tractions t
     ON t.id = td.tr_id
WHERE (td.sell_start_date >= '2015-09-10 01:00:00') AND
       confirm = 2 AND
       td.adviser = 7
GROUP BY coupon_id, seri, shadyab_profit, seri

您应该为所有列名添加适当的表别名。如果这些列中的任何一个都不明确,那么MySQL需要知道它们来自何处。

答案 3 :(得分:0)

使用包含更多列的派生表几乎没有意义。只需直接在表上进行分组而不使用不必要的子查询。 (如另一个答案中所述)您必须在所有列引用中包含相应的表别名(根据需要替换T?)

SELECT
      COUNT(T?.coupon_id) AS current_usr
    , T?.coupon_id
    , T?.shadyab_profit
    , T?.seri
FROM traction_details as TD
      INNER JOIN tractions as T ON TD.tr_id = T.id
WHERE TD.sell_start_date >= '2015-09-10 01:00:00'
      AND T?.confirm = 2
      AND TD.adviser = 7
GROUP BY
      T?.coupon_id
    , T?.seri
    , T?.shadyab_profit