1

我在 MySQL 数据库中有以下三个表,其结构如下:

tblinvoices

用户名 | 编号 | 税率 | 日期

tblinvoiceitems

用户名 | 重

tblcustomfield 值

回复 | 价值 | 字段ID

我想要做的是找到在特定日期范围内并且税率为 0 且 fieldid 为 5 且值包含字母的所有发票编号 (tblinvoices.id)。

这就是我使用代码尝试实现这一目标的程度,不幸的是它不起作用,我不明白为什么。我知道这相当复杂,所以任何建议都将不胜感激,我不确定我的方法是否正确。

SELECT DISTINCT id, date  
FROM `tblinvoices`  
INNER JOIN (SELECT userid,tblinvoiceitems.relid  
FROM  `tblinvoiceitems`  
INNER JOIN `tblcustomfieldsvalues`  
ON tblinvoiceitems.relid=tblcustomfieldsvalues.relid  
WHERE  `fieldid` =5  
AND  `value`   
REGEXP  '[A-Za-z]') tbl  
ON tblinvoices.userid=tbl.userid  
WHERE `taxrate` = 0  
AND `date`  
BETWEEN  '2012-07-01' AND '2012-09-31'  

简化但仍然不正确的结果:

SELECT DISTINCT i.id, i.date  
FROM `tblinvoices` i
INNER JOIN `tblinvoiceitems`  it ON it.userid=i.userid
INNER JOIN `tblcustomfieldsvalues`  cf ON it.relid=cf.relid  
WHERE  cf.`fieldid` =5  
AND  cf.`value` REGEXP  '[A-Za-z]'
AND i.`taxrate` = 0  
AND i.`date`  BETWEEN  '2012-07-01' AND '2012-09-31'  
GROUP BY i.id
4

1 回答 1

2

我只是简化您的查询。尝试这个

SELECT i.id, i.date  
FROM `tblinvoices` i
INNER JOIN `tblinvoiceitems`  it ON it.userid=i.userid
INNER JOIN `tblcustomfieldsvalues`  cf ON it.relid=cf.relid  
WHERE  cf.`fieldid` =5  
AND i.`taxrate` = 0  
AND i.`date`  BETWEEN  '2012-07-01' AND '2012-09-31'  
GROUP BY i.id

希望这会奏效

于 2012-10-11T15:27:49.590 回答