为什么这个MySQL连接查询会产生错误的结果?

时间:2012-05-16 21:54:22

标签: mysql

这是一个连接3个表的MySQL查询,但只有jobtitle表的结果被正确打印,为什么其他两个表是错误的重复结果。

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`

我该如何解决这个问题?

三个表的架构。

预订

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

JOBTITLE

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

JOBTITLE

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

当我运行上述查询时,我得到了这个输出:

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

1 个答案:

答案 0 :(得分:0)

要解决此问题,请删除GROUP BY子句。如果您想要消除使用GROUP BY返回的重复行,那么请在GROUP BY子句的SELECT列表中包含所有表达式。

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 `booking`.`receipt_no`, `client`, `operator`, `discount`, `total_amount`, `amount_paid`, `balance`, `date`, `jobtitle`, `quantity`, `amount`, `date_paid`, `old_balance`, `debtor_amount_paid`, `new_balance`

MySQL有一项功能,可以省略GROUP BY子句中的非聚合。要禁用此功能,并使MySQL像其他关系数据库一样工作:

SET sql_mode = 'ONLY_FULL_GROUP_BY';

[附录]

您问“我该如何解决这个问题”,但我们并未明确说明您希望返回的结果集。

我们完全期望在连接表之间存在一对多关系时会返回一些“重复”。

例如,假设我有一个order表和一个line_item表,一个订单可以在line_item中包含零个,一个或多个关联行,但是{{1} }与一个line_item相关联。当我运行如下查询时:

order

我希望每个与多个line_item关联的订单都返回o.id的“重复”值。

我强烈怀疑问题是使用“receipt_no”作为连接谓词。通常,连接谓词的格式为SELECT o.id, l.id FROM orders o JOIN line_item l ON l.order_id = o.id

我建议您运行一个从每个表中提取主键值的查询

child_table.parent_key = parent_table.key

这将显示您获得“重复”的位置。没有架构定义和一些示例数据,这就是我可以告诉你的全部内容。