如何为以下表编写sql查询

时间:2015-11-13 11:48:04

标签: mysql sql

enter image description here

我有一个表名为“invoice”,其中包含列名“CId”

enter image description here

我有另一个表名为invoiceclient_details,其中包含列名“CId”

所以现在我的问题是“我应该写什么查询以便我得到更大的数据”CId“,即由更大的”CID“组成的数据行

我试过这样的

SELECT
invoiceclient_details.OrganizationName,
invoiceclient_details.InvoiceNo,
invoiceclient_details.InvoiceDate,
invoiceclient_details.DeliveryNote,
invoiceclient_details.TermsofPayment,
invoiceclient_details.EsugamNo,
invoiceclient_details.OrganizationName,
invoiceclient_details.BuyerOrderNo,
invoiceclient_details.BuyDate,
invoiceclient_details.DispatchDocumentNo,
invoiceclient_details.Dated,
invoiceclient_details.DispatchThrough,
invoiceclient_details.Destination,
invoiceclient_details.TermsofDelivery,
invoiceclient_details.BuyerTin,
invoice.id,
invoice.DescriptionOfGoods,
invoice.Quantity,
invoice.PerUnitPrice,
invoice.TotalPrice,
invoice.VAT14,
invoice.VAT5,
invoice.ServiceTax,
invoice.CST
FROM invoiceclient_details,invoice
WHERE MAX(invoiceclient_details.CId) = MAX(invoice.CId);

但它显示的错误如

  

“群体功能的误用”

2 个答案:

答案 0 :(得分:1)

使用INNER JOIN加入CId上的表格。

SELECT *
FROM invoice i
INNER JOIN invoiceclient_details icd ON i.CId = icd.CId

答案 1 :(得分:0)

试试这个

select * 
from invoice,invoiceclient_details 
where 
invoice.CId in (Select max(invoiceclient_details.CId) from invoiceclient_details) 
and 
invoiceclient_details.CId in (Select max(invoiceclient_details.CId) from invoiceclient_details)