简体   繁体   English

Mysql SELECT…WHERE IN(从多个表中选择多个子)

[英]Mysql SELECT … WHERE IN (multiple sub selects from multiple tables)

Mysql need to get data from one table, where ids is the same as column values in another table. Mysql需要从一个表中获取数据,其中id与另一表中的列值相同。 But from the another table must get only column values, which ids are the same ( IN ) as column values in two other tables. 但是必须从另一个表中仅获取列值,其ID与其他两个表中的列值相同( IN )。

Tables are following: 表如下:

Table name sales_invoices with columns (below i show not all columns, there are columns, that necessary for the question). 具有列的表名称sales_invoices (在下面,我未显示所有列,而是显示了该问题所需的列)。
IdInvoices | IdInvoices | Date

Table name purchase_invoices with columns 表名称purchase_invoices与列
IdInvoicesP | IdInvoicesP | Date

Table name items with columns 带列的表名称items
IdI | IdI | IdGoodsServices | IdGoodsServices | IdInvoices

Table name goods_services with columns 表名称goods_services与列
IdGs (unique values) IdGs (唯一值)

Here are links among tables 这是表格之间的链接
IdGs = IdGoodsServices IdGs = IdGoodsServices
IdInvoices ( items ) = IdInvoices ( sales_invoices ) = IdInvoicesP IdInvoicesitems )= IdInvoicessales_invoices )= IdInvoicesP

Tried multiple variations of code. 尝试了代码的多种变体。 In all cases get errors. 在所有情况下,都会出错。 Below is the last used code. 下面是最后使用的代码。

SELECT count(DISTINCT `IdGs`) FROM `goods_services` WHERE `IdGs` IN 

  ( SELECT `IdGoodsServices` FROM `items` WHERE `IdInvoices` IN 

    ( SELECT `IdInvoices`, `IdInvoicesP` FROM 
    `sales_invoices`, `purchase_invoices` WHERE `Date` <= ? ) 

  )  

Want to count IdGs (codes of products or goods/services) that contains in sales and purchase invoices, issued (written) until certain date. 想要计算在特定日期之前发出(写)的销售和购买发票中包含的IdGs (产品或商品/服务代码)。

There is no direct linking between goods_services and sales_invoices / purchase_invoices . goods_servicessales_invoices / purchase_invoices之间没有直接链接。 Table items is linked to both goods_services and ( sales_invoices / purchase_invoices ) items被链接到两个goods_services和( sales_invoices / purchase_invoices

With the code above get error 与上面的代码得到错误
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'Date' in where clause is ambiguous

Tried sub-sub select only from one table, get some result and no error. 尝试从子表中仅选择一个子表,获得一些结果且没有错误。
Here is the query 这是查询

SELECT count(DISTINCT `IdGs`) FROM `goods_services` WHERE `IdGs` IN 

  ( SELECT `IdGoodsServices` FROM `30u2s5r_items` WHERE `IdInvoices` IN 

    ( SELECT `IdInvoicesP` FROM 
    `30u2s5r_purchase_invoices` WHERE `Date` <= ? ) 

  )  

Below is working code (as i tested until now). 下面是工作代码 (如我到目前为止测试的那样)。

SELECT count(DISTINCT `IdGs`) FROM `goods_services` WHERE `IdGs` IN 

 (SELECT `IdGoodsServices` FROM `items` WHERE 

 `IdInvoices` IN 

  (SELECT DISTINCT `IdInvoices` FROM `sales_invoices` 
  WHERE `sales_invoices`.`Date` <= ? ) 

   OR 

  `IdInvoices` IN 
  ( SELECT DISTINCT `IdInvoicesP` FROM `purchase_invoices` 
  WHERE `purchase_invoices`.`Date` <= ? ) 

)

使用Purchase_invoices.Date或sales_invoices.Date

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

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