三个表的结构。
预订
CREATE TABLE `booking` (
`bookingID` int(11) NOT NULL AUTO_INCREMENT,
`receipt_no` int(11) NOT NULL,
`client` varchar(32) NOT NULL,
`operator` varchar(32) NOT NULL,
`discount` int(11) NOT NULL,
`total_amount` int(64) NOT NULL,
`amount_paid` int(32) NOT NULL,
`balance` int(32) NOT NULL,
`date` date NOT NULL,
PRIMARY KEY (`bookingID`)
ENGINE=InnoDB AUTO_INCREMENT=209 DEFAULT CHARSET=latin1
职务
CREATE TABLE `jobtitle` (
`jobtitleID` int(11) NOT NULL AUTO_INCREMENT,
`jobtitle` varchar(255) NOT NULL,
`quantity` int(11) NOT NULL,
`amount` varchar(255) NOT NULL,
`jobtypeID` int(11) NOT NULL,
`bookingID` int(11) NOT NULL,
PRIMARY KEY (`jobtitleID`)
ENGINE=InnoDB AUTO_INCREMENT=463 DEFAULT CHARSET=latin1
First_graphics_debtors
CREATE TABLE `first_graphics_debtors`
`id` int(11) NOT NULL AUTO_INCREMENT,
`receipt_no` int(11) NOT NULL,
`date_paid` date NOT NULL,
`old_balance` int(32) NOT NULL,
`debtor_amount_paid` int(32) NOT NULL,
`new_balance` int(32) NOT NULL,
PRIMARY KEY (`id`)
ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1
插入少量数据后
预订
+-----------+------------+----------+----------+----------+--------------+-------------+---------+------------+
| bookingID | receipt_no | client | operator | discount | total_amount | amount_paid | balance | date |
+-----------+------------+----------+----------+----------+--------------+-------------+---------+------------+
| 205 | 156872940 | Osaro | Obi | 10 | 156380 | 135000 | 5742 | 2012-05-15 |
| 206 | 227349168 | Amaka | Stephen | 4 | 73250 | 70320 | 0 | 2012-05-15 |
| 207 | 155732278 | Aghahowa | Ibori | 0 | 116836 | 15000 | 101836 | 2012-05-15 |
| 208 | 753263343 | Chaka | Stephen | 10 | 231290 | 56000 | 152161 | 2012-05-15 |
+-----------+------------+----------+----------+----------+--------------+-------------+---------+------------+
职务
+------------+---------------------------+----------+--------+-----------+-----------+
| jobtitleID | jobtitle | quantity | amount | jobtypeID | bookingID |
+------------+---------------------------+----------+--------+-----------+-----------+
| 454 | A1 Full Colour | 10 | 4334 | 1 | 205 |
| 455 | Complementry Card | 20 | 5652 | 2 | 205 |
| 456 | A4 Printout (graphics)B/W | 25 | 2930 | 4 | 206 |
| 457 | Scan | 2 | 4334 | 5 | 207 |
| 458 | A4 Full Colour | 199 | 500 | 3 | 207 |
| 459 | ID Card | 2 | 4334 | 2 | 207 |
| 460 | A3 Full Colour | 10 | 4334 | 3 | 208 |
| 461 | Flex Banner | 20 | 2930 | 2 | 208 |
| 462 | A2 Single Colour | 199 | 650 | 1 | 208 |
+------------+---------------------------+----------+--------+-----------+-----------+
First_graphics_debtors
+----+------------+------------+-------------+--------------------+-------------+
| id | receipt_no | date_paid | old_balance | debtor_amount_paid | new_balance |
+----+------------+------------+-------------+--------------------+-------------+
| 7 | 156872940 | 2012-05-15 | 5742 | 5000 | 742 |
| 8 | 156872940 | 2012-05-15 | 5742 | 5742 | 0 |
| 9 | 753263343 | 2012-05-15 | 152161 | 152161 | 0 |
| 13 | 753263343 | 2012-05-15 | 152161 | 14524 | 137637 |
| 14 | 753263343 | 2012-05-15 | 152161 | 2000 | 150161 |
| 15 | 753263343 | 2012-05-15 | 152161 | 1000 | 151161 |
+----+------------+------------+-------------+--------------------+-------------+
当我运行这个查询时:
SELECT `booking`.`receipt_no`, `client`, `operator`, `discount`, `total_amount`,
`amount_paid`, `balance`, `date`, `jobtitle`, `quantity`, `amount`,
`date_paid`, `old_balance`, `debtor_amount_paid`, `new_balance`
FROM (`booking`)
JOIN `jobtitle` ON `jobtitle`.`bookingID` = `booking`.`bookingID`
JOIN `first_graphics_debtors`
ON `first_graphics_debtors`.`receipt_no` = `booking`.`receipt_no`
WHERE `booking`.`receipt_no` = '753263343'
AND `first_graphics_debtors`.`receipt_no` = '753263343'
GROUP BY `jobtitle`.`quantity`
我得到这个输出:
+------------+--------+----------+----------+--------------+-------------+---------+------------+------------------+----------+--------+------------+-------------+--------------------+-------------+
| receipt_no | client | operator | discount | total_amount | amount_paid | balance | date | jobtitle | quantity | amount | date_paid | old_balance | debtor_amount_paid | new_balance |
+------------+--------+----------+----------+--------------+-------------+---------+------------+------------------+----------+--------+------------+-------------+--------------------+-------------+
| 753263343 | Chaka | Stephen | 10 | 231290 | 56000 | 152161 | 2012-05-15 | A3 Full Colour | 10 | 4334 | 2012-05-15 | 152161 | 152161 | 0 |
| 753263343 | Chaka | Stephen | 10 | 231290 | 56000 | 152161 | 2012-05-15 | Flex Banner | 20 | 2930 | 2012-05-15 | 152161 | 152161 | 0 |
| 753263343 | Chaka | Stephen | 10 | 231290 | 56000 | 152161 | 2012-05-15 | A2 Single Colour | 199 | 650 | 2012-05-15 | 152161 | 152161 | 0 |
+------------+--------+----------+----------+--------------+-------------+---------+------------+------------------+----------+--------+------------+-------------+--------------------+-------------+
以下列的数据重复三次,而不是从receipt_no相关的四行中获取数据
date_paid, old_balance, debtor_amount_paid, new_balance
预期结果
+------------+--------+----------+----------+--------------+-------------+---------+------------+------------------+----------+--------+------------+-------------+--------------------+-------------+
| receipt_no | client | operator | discount | total_amount | amount_paid | balance | date | jobtitle | quantity | amount | date_paid | old_balance | debtor_amount_paid | new_balance |
+------------+--------+----------+----------+--------------+-------------+---------+------------+------------------+----------+--------+------------+-------------+--------------------+-------------+
| 753263343 | Chaka | Stephen | 10 | 231290 | 56000 | 152161 | 2012-05-15 | A3 Full Colour | 10 | 4334 | 2012-05-15 | 152161 | 152161 | 0 |
| 753263343 | Chaka | Stephen | 10 | 231290 | 56000 | 152161 | 2012-05-15 | Flex Banner | 20 | 2930 | 2012-05-15 | 152161 | 14524 | 137637 |
| 753263343 | Chaka | Stephen | 10 | 231290 | 56000 | 152161 | 2012-05-15 | A2 Single Colour | 199 | 650 | 2012-05-15 | 152161 | 2000 | 150161 |
+------------+--------+----------+----------+--------------+-------------+---------+------------+------------------+----------+--------+------------+-------------+--------------------+-------------+
虽然预期的输出不完整,但也许您可以从 debtor_amount_paid 和 new_balance 的数据中得到图片
最佳答案
我相当确定您的问题是因为您的 GROUP BY 子句。不确定您使用它的目的是什么,但请尝试不使用它。如果您得到重复的行,请尝试使用 SELECT DISTINCT。如果您的目标是根据该列对结果进行排序,请使用 ORDER BY。
此外,在 WHERE 子句中指定两次 receipt_no 是多余的。这两个表已经由该列连接,因此您只需在一个表中对其进行过滤即可。并且反引号并不是真正必要的,除了一些特殊的异常(exception),比如列名中的空格或逗号,或者与保留字同名的列(也可能是其他一些)。你的唯一一列看起来需要反引号是 date 列,但即使在该列中排除它们,你仍然应该没问题。我只是发现到处都是反引号会使查询变得更长且更难阅读。他们在那里不会伤害任何东西,所以如果你愿意,你可以离开他们,但就我个人而言,我不是粉丝。
我用上面提到的修改重写了你的查询,另外我给表别名以进一步缩短它。这不会提高性能或任何东西,只是让 IMO 更容易阅读:
SELECT DISTINCT
b.receipt_no, client, operator, discount, total_amount,
amount_paid, balance, `date`, jobtitle, quantity,
amount, date_paid, old_balance, debtor_amount_paid, new_balance
FROM
booking b
INNER JOIN jobtitle jt ON jt.bookingID = b.bookingID
INNER JOIN first_graphics_debtors fgd ON fgd.receipt_no = b.receipt_no
WHERE
b.receipt_no = '753263343'
ORDER BY
jt.quantity
关于mysql - 我无法让这个 mysql 连接查询产生所需的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10652009/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类