【发布时间】:2015-08-08 16:08:54
【问题描述】:
我正在尝试生成一个显示“已付款”发票的查询。我的查询不起作用,这意味着其中存在错误,或者我试图以这种方式实现的目标是不可能的。
// paid
$statement = "accounts_invoice i
INNER JOIN customer_type ct on i.invoice_customer_type = ct.customer_type_id
LEFT JOIN accounts_invoice_payment ip on i.invoice_id = ip.invoice_payment_invoice_id
WHERE invoice_posted='1'
AND (i.invoice_total_amount_exc_vat + i.invoice_total_vat_amount) = ip.TotalPayments"
. $where
. " ORDER BY invoice_id DESC";
$invoice_details_query = mysqli_query($con,
"SELECT i.*, ct.customer_type_name,
ip.invoice_payment_amount AS TotalPayments
FROM {$statement}
LIMIT {$startpoint} , {$per_page}")
or die(mysql_error());
在这种情况下$where 字符串为空,您可以根据需要将其删除。您也可以删除“LIMIT”,因为这纯粹是用于分页。
我只是想创建一个页面来显示已付或未付的发票,考虑到这在理论上听起来如此简单,这让我发疯了!
数据库结构;
-- phpMyAdmin SQL Dump
-- version 4.0.10.7
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Aug 08, 2015 at 05:17 PM
-- Server version: 5.1.73-cll
-- PHP Version: 5.4.31
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Database: `propsyst_atlas`
--
-- --------------------------------------------------------
--
-- Table structure for table `accounts_invoice`
--
CREATE TABLE IF NOT EXISTS `accounts_invoice` (
`invoice_id` int(11) NOT NULL AUTO_INCREMENT,
`invoice_customer_type` tinyint(4) DEFAULT NULL,
`invoice_customer` int(11) DEFAULT NULL,
`invoice_date` date DEFAULT NULL,
`invoice_due_date` date DEFAULT NULL,
`invoice_property_id` int(11) DEFAULT NULL,
`invoice_tenancy_id` int(11) DEFAULT NULL,
`invoice_branch` int(11) DEFAULT NULL,
`invoice_payment_terms` tinyint(4) DEFAULT NULL,
`invoice_notes` text COLLATE utf8_bin,
`invoice_total_amount_exc_vat` decimal(10,2) DEFAULT NULL,
`invoice_total_vat_amount` decimal(10,2) DEFAULT NULL,
`invoice_posted` tinyint(4) DEFAULT '0',
`invoice_date_created` datetime DEFAULT NULL,
`invoice_date_updated` datetime DEFAULT NULL,
`invoice_date_posted` datetime DEFAULT NULL,
`invoice_created_by` int(11) DEFAULT NULL,
`invoice_updated_by` int(11) DEFAULT NULL,
`invoice_posted_by` int(11) DEFAULT NULL,
PRIMARY KEY (`invoice_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=87 ;
-- phpMyAdmin SQL Dump
-- version 4.0.10.7
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Aug 08, 2015 at 05:18 PM
-- Server version: 5.1.73-cll
-- PHP Version: 5.4.31
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Database: `propsyst_atlas`
--
-- --------------------------------------------------------
--
-- Table structure for table `accounts_invoice_payment`
--
CREATE TABLE IF NOT EXISTS `accounts_invoice_payment` (
`invoice_payment_id` int(11) NOT NULL AUTO_INCREMENT,
`invoice_payment_date` date DEFAULT NULL,
`invoice_payment_amount` decimal(10,2) DEFAULT NULL,
`invoice_payment_method` tinyint(4) DEFAULT NULL,
`invoice_payment_invoice_id` int(11) DEFAULT NULL,
`invoice_payment_notes` text COLLATE utf8_bin,
`invoice_payment_date_created` datetime DEFAULT NULL,
`invoice_payment_date_updated` datetime DEFAULT NULL,
`invoice_payment_created_by` int(11) DEFAULT NULL,
`invoice_payment_updated_by` int(11) DEFAULT NULL,
PRIMARY KEY (`invoice_payment_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=71 ;
-- phpMyAdmin SQL Dump
-- version 4.0.10.7
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Aug 08, 2015 at 05:58 PM
-- Server version: 5.1.73-cll
-- PHP Version: 5.4.31
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Database: `propsyst_atlas`
--
-- --------------------------------------------------------
--
-- Table structure for table `customer_type`
--
CREATE TABLE IF NOT EXISTS `customer_type` (
`customer_type_id` int(11) NOT NULL AUTO_INCREMENT,
`customer_type_name` varchar(20) COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`customer_type_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=4 ;
【问题讨论】:
-
您是否遇到任何 MySQL 错误?在不了解数据库设计的情况下很难判断您的查询是否正确。
-
请为您的查询中使用的表创建复制 DDL。谢谢。
-
对不起。没有 MySQL 错误,我刚刚发布了表结构。
-
FROM accounts_invoice i INNER JOIN customer_type ct... 名为customer_type的表在哪里? -
浮点数很难比较,因为 db 不会将它们存储为精确值 - manual。我会使用整数来存储金额。在显示时格式化为逗号值。