Php 是否可以进行“已付或未付发票”MySQL查询?

Php 是否可以进行“已付或未付发票”MySQL查询?,php,mysql,Php,Mysql,我正在尝试生成一个显示“已付款”发票的查询。我的查询不起作用,这意味着它要么有错误,要么我试图通过这种方式实现的目标是不可能的 // paid $statement = "accounts_invoice i INNER JOIN customer_type ct on i.invoice_customer_type = ct.customer_type_id LEFT JOIN accounts_invoice_payment

我正在尝试生成一个显示“已付款”发票的查询。我的查询不起作用,这意味着它要么有错误,要么我试图通过这种方式实现的目标是不可能的

// 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字符串为空,如果愿意,可以将其删除。您还可以删除“限制”,因为它纯粹用于分页

我只是想制作一个页面来显示已付款或未付款的发票,考虑到它在理论上听起来如此简单,我简直快疯了

数据库结构

-- 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 ;

实际上,您的查询是

SELECT
    i.*,
    ip.invoice_payment_amount AS TotalPayments
FROM accounts_invoice i 
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
ORDER BY invoice_id DESC
该查询失败,因为where语句中的ip.TotalPayments是未知列。您需要使用原始名称ip.invoice\u payment\u amount。别名仅在返回的集合中使用,而不在查询本身中使用

此外,还可以改进数据库设计。首先,我建议使用InnoDB而不是MyISAM。这允许您使用外键,因此表之间实际上存在一种关系

我还要将WHERE条款的和部分移至ON条款,如下所示:

SELECT
    i.*,
    ip.invoice_payment_amount AS TotalPayments
FROM accounts_invoice i 
LEFT JOIN accounts_invoice_payment ip
    ON i.invoice_id = ip.invoice_payment_invoice_id 
    AND (i.invoice_total_amount_exc_vat + i.invoice_total_vat_amount) = ip.invoice_payment_amount
WHERE invoice_posted='1' 
ORDER BY invoice_id DESC

从A.id=B.A_id上的左联接B中选择*的结果包含A的每一行和B的匹配行。如果B没有A中某一行的匹配行,则B中各列的值为空。如果您不希望这样做,而是只希望A的行在B中有匹配的行,那么应该使用内部联接。

您是否收到任何MySQL错误?如果不知道数据库设计,很难判断查询是否正确。请复制DDL以创建查询中使用的表。谢谢,对不起。没有MySQL错误,我刚刚发布了表结构。从accounts\u invoice I INNER JOIN customer\u type ct。。。名为customer_type的表在哪里?浮点数很难比较,因为db不会将它们存储为精确值-。我会使用整数来存储数据。在显示时格式化为逗号值。感谢您的帮助。您的查询正常,没有错误,但同时显示已付款和未付款发票。忽略上述内容,我从未将左连接更改为内部连接!你能试试吗:选择i.*,ct.customer\u type\u name,ip.invoice\u payment\u Amounts作为来自testforstack.accounts\u invoice i内部联接testforstack.customer\u type=ct.customer\u type\u id左联接testforstack.accounts\u invoice\u payment ip on i.invoice\u id=ip.invoice\u payment\u invoice\u id何处发票过账='1'和i.发票总额+增值税+i.发票总额=增值税=ip.发票付款金额按发票id说明排序;将testforstack替换为您的数据库名称。@Arjan如何更改查询以显示未付发票?我已尝试将其更改为和I.invoice\u total\u amount\u exc\u vat+I.invoice\u total\u vat\u amount=ip.invoice\u payment\u amount,并将其设置为WHERE查询,但未返回任何结果。