-1

这是一个连接 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

职称

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

当我运行上述查询时,我得到以下输出:

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

1 回答 1

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,但 aline_item与一个关联order。当我运行如下查询时:

SELECT o.id, l.id
  FROM orders o
  JOIN line_item l ON l.order_id = o.id

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

我强烈怀疑问题在于使用“receipt_no”作为连接谓词。通常,连接谓词的形式为 child_table.parent_key = parent_table.key

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

SELECT b.bookingID
     , j.ID
     , d.ID
  FROM booking b
  JOIN jobtitle j ON j.bookingID = b.bookingID
  JOIN first_graphics_debtors d ON d.receipt_no = b.receipt_no
 WHERE b.receipt_no = '753263343'
ORDER BY 1,2,3

这将告诉你你在哪里得到“重复”。如果没有模式定义和一些示例数据,我只能告诉你这些。

于 2012-05-16T22:02:50.217 回答