MySQL联接从结果中删除重复项而不使用分组依据

时间:2019-04-16 13:15:49

标签: mysql group-by duplicates where

我有3张桌子:

table "f" (26000 record)
+------------------+------------------+------+-----+---------+-------+
| Field            | Type             | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+-------+
| idFascicolo      | int(11)          | NO   | PRI |         |       |
| oggetto          | varchar          | NO   |index|         |       |
+------------------+------------------+------+-----+---------+-------+

table "r" (22000 record)
+------------------+------------------+------+-----+---------+-------+
| Field            | Type             | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+-------+
| idRichiedente    | int(11)          | NO   | PRI |         |       |
| name             | varchar          | NO   |index|         |       |
+------------------+------------------+------+-----+---------+-------+

table "fr" (32000 record)
+------------------+------------------+------+-----+---------+-------+
| Field            | Type             | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+-------+
| id               | int(11)          | NO   | PRI |         |       |
| idFascicolo      | int(11)          | NO   |index|         |  FK   |
| idRichiedente    | int(11)          | NO   |index|         |  FK   |
+------------------+------------------+------+-----+---------+-------+

这是我的选择:

SELECT
f.idFascicolo,
f.oggetto,
r.richiedente
FROM fr
JOIN f ON (f.idFascicolo=fr.idFascicolo)
JOIN r ON (r.idRichiedente=fr.idRichiedente)
WHERE r.name LIKE 'string%'

如何在不使用GROUP BY的情况下删除结果中的重复项(即“ Rossi Mario”和“ Rossi Marco”可以是f.idFascicolo的两个r.name)?

如何使用WHERE子句删除重复项?!

this post

建议不要使用GROUP BY,但是我不知道该怎么做! 非常感谢!

数据示例:

"r" data
+---------------+------------------------
| idRichiedente |   name                |
+---------------+-----------------------+
| 1             |   Rossi Mario         |
+---------------+-----------------------+
| 2             |   Rossi Marco         |
+---------------+-----------------------+

"f" data
+---------------+-----------------------+
| idFascicolo   |   oggetto             |
+---------------+-----------------------+
| 1             |   oggetto1            |
+---------------+-----------------------+
| 2             |   oggetto2..o         |
+---------------+-----------------------+

"fr" data
+---------------+-----------------------+-----------------+
| id            |   idFascicolo         |   idRichiedente |
+---------------+-----------------------+-----------------+
| 1             |   1                   | 1               |
+---------------+-----------------------+-----------------+
| 2             |   1                   | 2               |
+---------------+-----------------------+-----------------+

0 个答案:

没有答案