【问题标题】:SQL SELECT and join multiple tables erron in query?SQL SELECT并在查询中连接多个表错误?
【发布时间】:2021-06-23 14:23:34
【问题描述】:

我的查询按预期工作:

        $sql = '
SELECT s.rowid
     , f.fk_soc
     , s.nom
     , f.datef
     , sc.fk_soc
     , sc.fk_user
     , u.rowid
     , u.firstname
     , u.lastname
  FROM societe s
  JOIN societe_commerciaux sc 
    ON s.rowid = sc.fk_soc
  JOIN user u 
    ON sc.fk_user = u.rowid
  LEFT 
  JOIN facture f 
    ON s.rowid = f.fk_soc
 GROUP 
    BY s.rowid
HAVING MAX(f.datef) <= CURRENT_TIMESTAMP - INTERVAL '.$db->escape($dlo).' DAY
';
        $resql = $db->query($sql);
        if (!$resql) {
            dol_print_error($db);
        }

背景:

user - 包含用户名和姓的表以及主标识。 rowid

Societe - 带有客户名称 s.nom 主标识的表。 rowid

facture - 包含客户发票的表格,日期为 datef 和 fk_soc,这是客户 ID (s.rowid)

societe_commerciaux - 通过将fk_soc(实际上是 s.rowid)与 fk_user(即 u.rowid)匹配来建立代表每个客户端的用户的连接

工作脚本列出了在$dlo 和销售代表姓名设置的期间内没有发票的所有客户。

我想要做的是,调整结果以向没有发票的客户显示特定销售代表(即用户)。

我有一个工作脚本,填充下拉选择

    <form>
         <select name='repselect' onchange='showUser(this.value)'>
             <option value="here comes the value for id of sales representative 1">Sales Representative name 1</option>
             <option value="here comes the value for id of sales representative 2">Sales Representative name 2</option>
         </select>
    </form>

Value 字段中的 ID 是来自 table users 的 bot rowid 和来自 table societe_commerciaux 的 fk_ser。

我还有一个有效的 ajax 脚本来填充结果。

脚本的工作方式就像我在其中输入了print $sql;,在选择用户后我可以看到查询。

我正在苦苦挣扎的是如何调整上述查询以仅显示从下拉列表中选择的销售代表的结果。我将此 id 传递给一个文件“script.php”,其中有$userid = intval($_GET['q']);,后跟查询。我可以处理输出。这只是我无法正确调整的查询。

【问题讨论】:

  • 如果 GROUP BY f.fk_soc , s.nom , f.datef , sc.fk_soc , sc.fk_user , u.rowid , u.firstname , u.lastname 做的事情与 GROUP BY s.rowid 不同,那么事情会很糟糕。
  • 按照建议,完成了这项工作。现在工作。在JOIN societe_commerciaux sc ON s.rowid = sc.fk_soc之后添加
  • 嗯?好吧,如果它有效,我与谁争论?

标签: php mysql sql ajax dropdown


【解决方案1】:

此查询返回上次发票(实际)日期小于 (CURRENT_TIMESTAMP - INTERVAL) 或从未有发票的客户的用户代表信息:

$sql = '

SELECT 
  u.rowid,
  u.firstname,
  u.lastname
     
FROM societe s
INNER JOIN societe_commerciaux sc ON s.rowid = sc.fk_soc
INNER JOIN user u ON sc.fk_user = u.rowid

WHERE s.rowid NOT IN (
           SELECT f.fk_soc
           FROM facture f
           GROUP BY f.fk_soc
           HAVING MAX(f.datef) > CURRENT_TIMESTAMP - INTERVAL ' . $db->escape($dlo) . ' DAY)

GROUP BY u.rowid, u.firstname, u.lastname
';

您应该使用准备好的语句,并且不要在普通查询中粘贴参数 $dlo,以防止 sql 注入。

【讨论】:

    【解决方案2】:

    这是我使用的最后一个查询:

    SELECT ';
        s.rowid, 
        s.nom, 
        s.phone, 
        s.email, 
        sc.fk_soc, 
        sc.fk_user, 
        u.rowid, 
        u.firstname, 
        u.lastname, 
        f.max_datef
    FROM 
        ' . MAIN_DB_PREFIX . 'societe s
    INNER JOIN 
        ' . MAIN_DB_PREFIX . 'societe_commerciaux sc ON s.rowid = sc.fk_soc
        AND sc.fk_user="' . $this->userId . '"
    INNER JOIN 
        ' . MAIN_DB_PREFIX . 'user u ON sc.fk_user = u.rowid
    INNER JOIN (
        SELECT Max($this->typeDate) As max_datef, fk_soc
        FROM
        ' . MAIN_DB_PREFIX . 'facture
        GROUP BY fk_soc
        'HAVING max_datef <= CURRENT_TIMESTAMP - INTERVAL $this->interval DAY
    ) f ON s.rowid = f.fk_soc
    ORDER BY max_datef ASC
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-27
      • 1970-01-01
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      • 2012-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多