简体   繁体   English

mysql join时如何查入另一张表没有数据?

[英]How to check in another table has no data while doing mysql join?

I have two mysql table.我有两个 mysql 表。

Invoice_details

invoice_no   proje_ID invoice_Date invoice_amount
1004             6
1005             4
1006             2
1007             2
1008             1
1009             7

received_amount_details 

invoice_no        received_amount
1004                 205.00
1004                  40.00
1006                 283.50

Expected ouput: select from invoice_details where proje_ID =6
invoice no        invoice_amount   invoice_Date  received_amount
1004                                                  245.00

Expected ouput: select from invoice_details where proje_ID =2
invoice no        invoice_amount   invoice_Date  received_amount
1006                                                   283.50
1007                                                    0.00 (becoz 1007 invoice is not there in another table called received_amount_details so show total sum 0.00)

Expected ouput: select from invoice_details where proje_ID =7
    invoice no        invoice_amount   invoice_Date  received_amount
    1009                                                  0.00

Here is my query:这是我的查询:

public function many($id) {
   $sql = "SELECT invoice_details.invoice_No, invoice_details.invoice_Date, invoice_details.invoice_amount,
        SUM(received_amount_details.received_amount) as totalreceiptamt
        FROM 
          (SELECT DISTINCT invoice_no, received_amount FROM received_amount_details) AS received_amount_details 
      
         INNER JOIN invoice_details ON received_amount_details.invoice_no = invoice_details.invoice_No
         where invoice_details.proje_ID='7'
         group by invoice_details.invoice_No";

        return $this->db->query($sql)->result();
            }

currently am getting an empty result if I select invoice details where proje_ID='7' Where i have to change in my php code?如果我 select 发票详细信息 where proje_ID='7' 我必须在我的 php 代码中更改哪里?

When I select an invoice (invoice no:1004) it will display all the invoice details and sum of the total received_amount (205.00 + 40.00).But if select invoice (invoice no:1007) am getting null data becoz this 1007 invoice no is not there in second table.当我 select 发票(发票编号:1004)时,它将显示所有发票详细信息和总 received_amount 的总和(205.00 + 40.00)。但是如果 select 发票(发票编号:1007)正在获取 null 数据,因为此 1007 发票编号是第二张桌子上没有。 If it is not there in the received_amount_details table just return the invoice details and total received_amount =0.00.如果它不在 received_amount_details 表中,只需返回发票详细信息和总 received_amount =0.00。 (I want to display the invoice details where project id =2 and total received_amount =0.00.) (我想显示项目 ID = 2 和总接收金额 =0.00 的发票详细信息。)

Add IF condition in SQL query, Something like this.在 SQL 查询中添加 IF 条件,类似这样。

SELECT invoice_details.invoice_no,
       invoice_details.invoice_date,
       invoice_details.invoice_amount,
       SUM(IF(received_amount_details.received_amount IS NULL
               OR received_amount_details.received_amount = '', 0.0,
               received_amount_details.received_amount)) AS totalreceiptamt
FROM   (SELECT DISTINCT invoice_no,
                        received_amount
        FROM   received_amount_details) AS received_amount_details
       right join invoice_details
               ON received_amount_details.invoice_no =
                  invoice_details.invoice_no
WHERE  invoice_details.proje_id = '7'
GROUP  BY invoice_details.invoice_no 

This will add 0.0 as default value is not received_amount_details.received_amount IS NULL or blank这将添加 0.0,因为默认值不是 received_amount_details.received_amount IS NULL 或空白

Also we're doing right join to get all values from invoice_details page.此外,我们正在执行 right join 以从 invoice_details 页面获取所有值。

You can learn more about IF condition in MYSQL here,您可以在 MYSQL 中了解有关 IF 条件的更多信息,

https://www.w3schools.com/mysql/func_mysql_if.asp https://dev.mysql.com/doc/refman/8.0/en/if.html https://www.w3schools.com/mysql/func_mysql_if.asp https://dev.mysql.com/doc/refman/8.0/en/if.html

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM