多次返回单行

时间:2013-01-04 06:34:31

标签: python mysql

我有两张这样的表:

summary(id, status, value, time, aperson) and long_summary(id, who, comment, alltext)

因为我尝试了下面的查询,如:

SELECT l.id,summary.status
FROM  long_summary l INNER JOIN summary ON l.id = summary.id INNER JOIN long_summary ON summary.aperson = long_summary.who 
WHERE summary.status IN('old','new','waiting')
AND summary.value IN ('') 
AND summary.time >= DATE_SUB(NOW(),INTERVAL 2 MONTH)
AND l.alltext LIKE '% relational database management system %' ORDER BY FIELD(summary.status,'old','new','waiting'),summary.time DESC

在运行它时会提供正确的输出但是已经多次返回的行如下:

(1,'old')
(1,'old')
(1,'old')
(2,'new')
(2,'new')
(3,'new')
(4,'waiting')
(4,'waiting')

对于那个i have used SELECT DISTINCT and after that it is giving correct output without repetition of the single row。但我没有得到我做错了什么? 你能建议吗?

1 个答案:

答案 0 :(得分:1)

查看正在发生的事情的方法是(a)创建一个精简的测试用例,然后(b)执行SELECT *,这样你就可以看到你在其他列中得到的内容,这会导致重复。

所以,我这样做了:

CREATE TABLE summary(id, status, value, time, aperson);
INSERT INTO "summary" VALUES(1,'old',23,'time0','joe');
INSERT INTO "summary" VALUES(2,'new',42,'time1','bob');
INSERT INTO "summary" VALUES(3,'new',32,'time2','mike');
CREATE TABLE long_summary(id, who, comment, alltext);
INSERT INTO "long_summary" VALUES(1,'someone','i say!','some text');
INSERT INTO "long_summary" VALUES(1,'joe','joe likes','some text');
INSERT INTO "long_summary" VALUES(2,'joe','joe likes bob','some text');
INSERT INTO "long_summary" VALUES(3,'joe','joe likes mike','some text');
INSERT INTO "long_summary" VALUES(1,'bob','nice one, joe','some text');
INSERT INTO "long_summary" VALUES(2,'bob','nice one, me','some text');
INSERT INTO "long_summary" VALUES(2,'bob','double nice one, me','some text');
INSERT INTO "long_summary" VALUES(3,'bob','nice one, mike','some text');
COMMIT;

然后我提出了一个更简单的查询版本:

SELECT l.id,summary.status
FROM  long_summary l INNER JOIN summary ON l.id = summary.id INNER JOIN long_summary ON summary.aperson = long_summary.who 
WHERE summary.status IN('old','new','waiting')

其他任何东西都不会让事情变得更糟,对吧?所以这是无关紧要的。运行时我能得到什么? 9份1|old份和12份2|new

所以,让我们改变它以查看整行:

SELECT *
FROM  long_summary l INNER JOIN summary ON l.id = summary.id INNER JOIN long_summary ON summary.aperson = long_summary.who 
WHERE summary.status IN('old','new','waiting')

1|bob|nice one, joe|some text|1|old|23|time0|joe|1|joe|joe likes|some text
1|bob|nice one, joe|some text|1|old|23|time0|joe|2|joe|joe likes bob|some text
1|bob|nice one, joe|some text|1|old|23|time0|joe|3|joe|joe likes mike|some text
1|joe|joe likes|some text|1|old|23|time0|joe|1|joe|joe likes|some text
1|joe|joe likes|some text|1|old|23|time0|joe|2|joe|joe likes bob|some text
1|joe|joe likes|some text|1|old|23|time0|joe|3|joe|joe likes mike|some text
1|someone|i say!|some text|1|old|23|time0|joe|1|joe|joe likes|some text
1|someone|i say!|some text|1|old|23|time0|joe|2|joe|joe likes bob|some text
1|someone|i say!|some text|1|old|23|time0|joe|3|joe|joe likes mike|some text
2|bob|double nice one, me|some text|2|new|42|time1|bob|1|bob|nice one, joe|some text
2|bob|double nice one, me|some text|2|new|42|time1|bob|2|bob|double nice one, me|some text
2|bob|double nice one, me|some text|2|new|42|time1|bob|2|bob|nice one, me|some text
2|bob|double nice one, me|some text|2|new|42|time1|bob|3|bob|nice one, mike|some text
2|bob|nice one, me|some text|2|new|42|time1|bob|1|bob|nice one, joe|some text
2|bob|nice one, me|some text|2|new|42|time1|bob|2|bob|double nice one, me|some text
2|bob|nice one, me|some text|2|new|42|time1|bob|2|bob|nice one, me|some text
2|bob|nice one, me|some text|2|new|42|time1|bob|3|bob|nice one, mike|some text
2|joe|joe likes bob|some text|2|new|42|time1|bob|1|bob|nice one, joe|some text
2|joe|joe likes bob|some text|2|new|42|time1|bob|2|bob|double nice one, me|some text
2|joe|joe likes bob|some text|2|new|42|time1|bob|2|bob|nice one, me|some text
2|joe|joe likes bob|some text|2|new|42|time1|bob|3|bob|nice one, mike|some text

好的,现在你可以看到问题,让我们看看为什么正在发生。这些行中的每一行都应该吗?换句话说,如果前三个组合中至少有一个存在,你应该看到1|old吗?

如果没有,哪些不应该造成它?您需要在WHEREJOIN中过滤掉某些内容。

如果是这样,那么您需要GROUP BY将相关字段合并在一起,或者在某处合并OR,更可能是第一个。

逐步通过所有提出相同问题的小组。如果您在实际显示的列上需要GROUP BY,则只需使用SELECT DISTINCT即可。

您也可能想退后一步,询问您是否真的需要一个完整的JOIN long_summary以及带有long_summary的摘要。这是8 * 3 * 8 = 192行,你已经过滤到21。这是否有意义,或者你只是期望,比方说,24行过滤?如果是后者,那你就错了JOIN。这些JOIN中的任何一个都不应该存在,或者它应该是一对一而不是一对多JOIN,或者其他错误。< / p>

顺便说一句,您可以从上面的测试中看出我使用的是sqlite3而不是mysql,因为它更容易获取和运行。我怀疑这有什么不同,但如果你在mysql测试并看到不同的结果,请务必告诉我。