简体   繁体   English

表中的 Select 行,其中 id 等于另一个表中的另一个 id,并对结果列中的值求和

[英]Select rows in a table where id is equals to another id in another table and sum values in column from the result

I have a small situation here.我这里有一个小情况。 I have 2 tables "facture" and "facturedet".我有 2 个表“facture”和“facturedet”。

I can get this query working correctly:我可以让这个查询正常工作:

SELECT * 
FROM facturedet  
WHERE facture_id IN (SELECT rowid 
                     FROM facture 
                     WHERE CAST(date_valid AS DATE) = current_date)

Now, in the result of the above query, I have a column "qty".现在,在上述查询的结果中,我有一列“数量”。

How can I sum the values in this column?如何求和此列中的值? I've tried我试过了

SELECT SUM(qty) 
FROM
    (SELECT * 
     FROM llx_facturedet 
     WHERE fk_facture IN (SELECT rowid 
                          FROM llx_facture 
                          WHERE CAST(date_valid AS DATE) = current_date))

I would suggest JOIN and GROUP BY :我建议JOINGROUP BY

SELECT SUM(fd.qty)
FROM facturedet fd JOIN
     facture f
     ON fd.facture_id = f.rowid
WHERE date_valid >= current_date AND
      date_valid < current_date INTERVAL '1 DAY';

Note: This uses inequalities for the date comparison.注意:这使用不等式进行日期比较。 The exact method for adding one date varies by database.添加一个日期的确切方法因数据库而异。 In most databases, this structure for the comparison is better for the optimizer, which means a faster query.在大多数数据库中,这种比较结构更适合优化器,这意味着查询速度更快。

This is the final query I am using:这是我使用的最终查询:

Select
    COALESCE(SUM(CASE WHEN f.type = 2 THEN -fd.qty ELSE fd.qty END), 0) As qty_sum
From
    ' . MAIN_DB_PREFIX . 'facturedet fd 
Inner Join
    ' . MAIN_DB_PREFIX . 'facture f On fd.fk_facture = f.rowid 
Inner Join
    ' . MAIN_DB_PREFIX . 'societe_commerciaux sc On sc.fk_soc = f.fk_soc 
Inner Join
    ' . MAIN_DB_PREFIX . 'user u On sc.fk_user = u.rowid
Where
    f.$this->typeDate Between $this->startDate And $this->endDate 
And
    u.rowid = "' . $this->userId . '"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从表中选择行,其中具有相同id的另一个表中的行在另一列中具有特定值 - Select rows from a table where row in another table with same id has a particular value in another column 如何 SQL 中另一个表中具有相同 id 的值的 select 总和 - How to select sum of the values with same id from another table in SQL 选择名为Titel的列,其中的ID与另一个表中的ID相同 - Select column called Titel where the id is the same from another Table 从ID不在另一个表中选择 - Select from table where id is not in another 选择一个id不在另一个表中的地方 - SELECT where an id is not in another table 如何通过从另一张表中具有相同id的行中的列中获取值来将值插入到连续行中的表的列中 - how to insert values into a column of a table in successive rows by fetching values from a column in a row with a same id in another table 从表中选择,其中 id 没有用另一个 user_id 表示 - SELECT from table where id is not represented with another user_id 从ID列表在另一个表上的表中选择* - Select * from table where ID list is on another table 从表中选择字段,其中id不在mysql的另一个表中[不起作用] - select fields from table where id not in another table in mysql [not working] mysql从一个表中选择count行,其中列与另一个表中的值类似 - mysql select count rows from a table where column like values from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM