- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试生成一个将显示“已支付”发票的查询。我的查询不起作用,这意味着其中存在错误,或者我试图通过这种方式实现的目标是不可能的。
// 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 ;
最佳答案
实际上你的查询是
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_payment_amount
。别名仅用于返回的集合,而不用于查询本身。
此外,您的数据库设计还可以改进。首先,我建议使用 InnoDB
而不是 MyISAM
。这允许您使用外键,因此表之间实际上存在关系。
另外,我会将 WHERE
子句的 AND
部分移至 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
SELECT * FROM A LEFT JOIN B ON A.id = B.a_id
的结果包含 A
的每一行和 B< 的匹配行
。如果 B
没有与 A
中的行匹配的行,则 B 中列的值为 NULL
。如果您不希望这样,而只想要 A
的行在 B
中具有匹配行,则您应该使用 INNER JOIN
。
关于php - 这个 'paid or unpaid invoices' MySQL 查询可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31895937/
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
使用 jquery,如果我有各种成员(member)选项。如果成员(member)级别当前显示为“未付费”,我可以显示某个 div 吗? Membership Level Unpa
我正在尝试生成一个将显示“已支付”发票的查询。我的查询不起作用,这意味着其中存在错误,或者我试图通过这种方式实现的目标是不可能的。 // paid $statement = "accounts_inv
示例: select orderid from `orders` where sum(price) 对于 1 号答案可能按预期工作,但代码中存在一些错误,因为它没有生成所需的结果,并且当我还添加 T
新查询 $query = "SELECT COUNT(*) FROM payments $sCriteria"; $result = mysql_query($query) or di
我是一名优秀的程序员,十分优秀!