请考虑以下事项:
eventTypes表有163行。
事件有43,000行。
SELECT events.eventTypeID, eventTypes.eventTypeName
FROM events
LEFT JOIN eventTypes ON events.eventTypeID = eventTypes.eventTypeID
返回163行。但是,如果我添加“WHERE events.eventID> = 0”
SELECT events.eventTypeID, eventTypes.eventTypeName
FROM events
LEFT JOIN eventTypes ON events.eventTypeID = eventTypes.eventTypeID
WHERE events.eventID >= 0
我得到了所有43,000行。我希望缺少一个WHERE子句会给我一切。我在考虑这个错误吗?
更新:我刚在另一台服务器上尝试了这个并且结果相同。我复制并粘贴的确切查询是:
SELECT events.eventTypeID, eventTypes.eventTypeName FROM events LEFT JOIN eventTypes ON events.eventTypeID = eventTypes.eventTypeID
这只返回前163条记录。 MySQL版本是5.5.29和5.1.61。我查看了错误列表但没有找到任何内容。
更新#2:EXPLAIN为任一查询提供相同的输出(即有或没有WHERE 1 = 1)
mysql> EXPLAIN(SELECT events.eventTypeID, eventTypes.eventTypeName FROM events LEFT JOIN eventTypes ON events.eventTypeID = eventTypes.eventTypeID);
+----+-------------+------------+--------+---------------+-------------+---------+-------------------------------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+-------------+---------+-------------------------------+-------+-------------+
| 1 | SIMPLE | events | index | NULL | eventTypeID | 4 | NULL | 37748 | Using index |
| 1 | SIMPLE | eventTypes | eq_ref | PRIMARY | PRIMARY | 4 | casefriend.events.eventTypeID | 1 | |
+----+-------------+------------+--------+---------------+-------------+---------+-------------------------------+-------+-------------+
更新#3在第三个系统上进行测试会产生我期望的结果,尽管我不知道为什么。第三个系统是运行MySQL 5.1.69的另一个CentOS6。我从我的开发系统中导入了确切的转储,导入到第二个测试系统,但没有产生正确的结果。
答案 0 :(得分:1)
我在SqlFiddle上试过这个:http://sqlfiddle.com/#!2/c9908b/1
create table event (id int,type_id int);
create table type (type_id int, type_name varchar(30));
insert into type values(1, 'type 1');
insert into type values(2, 'type 2');
insert into type values(3, 'type 3');
insert into type values(4, 'type 4');
insert into type values(5, 'type 5');
insert into event values( 1,1);
insert into event values( 2,1);
insert into event values( 3,1);
insert into event values( 4,1);
insert into event values( 5,2);
insert into event values( 6,2);
insert into event values( 7,2);
insert into event values( 8,2);
insert into event values( 9,3);
insert into event values(10,3);
insert into event values(11,3);
insert into event values(12,3);
insert into event values(13,4);
insert into event values(14,4);
insert into event values(15,4);
insert into event values(16,4);
insert into event values(17,5);
insert into event values(18,5);
insert into event values(19,5);
insert into event values(20,5);
select event.id, type.type_name from event left join type
on event.type_id=type.type_id
我按预期回来了20行
答案 1 :(得分:0)
SELECT events.eventTypeID, eventTypes.eventTypeName
FROM events
LEFT JOIN eventTypes ON events.eventTypeID = eventTypes.eventTypeID
WHERE events.eventID >= 0
如果提及( events.eventID> = 0 ),则表示获取所有这些条件为true且事件表具有43000行并且条件为所有行且您使用LEFT加入所以你得到43000行
答案 2 :(得分:0)
I would expect the lack of a WHERE clause would give me everything.
Am I thinking about this wrong?
是的,你是对的,你的第一个查询是正确的,它应该返回你期望的。
您正在使用LEFT OUTER JOIN
,这意味着左表中的所有记录和右表中的匹配记录。
这个问题与我所知的MYSQL版本无关。
我的建议是,请检查您加入两个表的关系(外键和主键)。