Php MySQL-为什么这个SELECT查询会给出多条记录
我正在刷新一些MySQL技能(希望如此) 我有一个疑问:Php MySQL-为什么这个SELECT查询会给出多条记录,php,mysql,Php,Mysql,我正在刷新一些MySQL技能(希望如此) 我有一个疑问: SELECT stops.name, stops.stop_id, stop_times.trip_id, stop_times.departure_time, trips.route_id FROM stops, routes, stop_times, trips WHERE stops.stop_id = stop_times.stop_id AND trips.route_id = 2 AND
SELECT stops.name,
stops.stop_id,
stop_times.trip_id,
stop_times.departure_time,
trips.route_id
FROM stops, routes, stop_times, trips
WHERE stops.stop_id = stop_times.stop_id AND trips.route_id = 2 AND stop_times.trip_id = 'SCHED255'
ORDER BY stops.stop_id ASC
这返回了正确的信息,但我得到了每条记录的多次出现。你知道我错过了什么吗?TIA在
FROM
-子句中有4个表。因此,您至少需要3个连接
,以避免数据的多次出现
基本规则:n个表=(n-1)连接
+++++++++++编辑+++++++++++
在您的情况下,它应该如下所示:
/*WITH EXCPLICIT JOINS*/
SELECT stops.name, stops.stop_id, stop_times.trip_id, stop_times.departure_time, trips.route_id
FROM stops s JOIN routes r ON s.col_name = r.col_name /*col_name is the name of the column that is defined as Contraint between these two*/
JOIN stop_times st ON st.stop_id = s.stop_id
JOIN trips t ON t.route_id = r.route_id
AND t.route_id = 2
AND st.trip_id = 'SCHED255'
ORDER BY s.stop_id ASC
/*THE SAME THING WITH IMPLICIT JOINS*/
SELECT stops.name, stops.stop_id, stop_times.trip_id, stop_times.departure_time, trips.route_id
FROM stops AS s, routes AS r, stop_times AS st, trips AS t
WHERE s.col_name = r.col_name /*JOIN of stops and trips*/
AND st.stop_id = s.stop_id /*JOIN of stop_times and stops*/
AND t.route_id = r.route_id /*JOIN of trips and rutes*/
AND t.route_id = 2 /*Other condition*/
AND st.trip_id = 'SCHED255' /*Other condition*/
ORDER BY s.stop_id ASC
在
FROM
-子句中有4个表。因此,您至少需要3个连接
,以避免数据的多次出现
基本规则:n个表=(n-1)连接
+++++++++++编辑+++++++++++
在您的情况下,它应该如下所示:
/*WITH EXCPLICIT JOINS*/
SELECT stops.name, stops.stop_id, stop_times.trip_id, stop_times.departure_time, trips.route_id
FROM stops s JOIN routes r ON s.col_name = r.col_name /*col_name is the name of the column that is defined as Contraint between these two*/
JOIN stop_times st ON st.stop_id = s.stop_id
JOIN trips t ON t.route_id = r.route_id
AND t.route_id = 2
AND st.trip_id = 'SCHED255'
ORDER BY s.stop_id ASC
/*THE SAME THING WITH IMPLICIT JOINS*/
SELECT stops.name, stops.stop_id, stop_times.trip_id, stop_times.departure_time, trips.route_id
FROM stops AS s, routes AS r, stop_times AS st, trips AS t
WHERE s.col_name = r.col_name /*JOIN of stops and trips*/
AND st.stop_id = s.stop_id /*JOIN of stop_times and stops*/
AND t.route_id = r.route_id /*JOIN of trips and rutes*/
AND t.route_id = 2 /*Other condition*/
AND st.trip_id = 'SCHED255' /*Other condition*/
ORDER BY s.stop_id ASC
在
FROM
-子句中有4个表。因此,您至少需要3个连接
,以避免数据的多次出现
基本规则:n个表=(n-1)连接
+++++++++++编辑+++++++++++
在您的情况下,它应该如下所示:
/*WITH EXCPLICIT JOINS*/
SELECT stops.name, stops.stop_id, stop_times.trip_id, stop_times.departure_time, trips.route_id
FROM stops s JOIN routes r ON s.col_name = r.col_name /*col_name is the name of the column that is defined as Contraint between these two*/
JOIN stop_times st ON st.stop_id = s.stop_id
JOIN trips t ON t.route_id = r.route_id
AND t.route_id = 2
AND st.trip_id = 'SCHED255'
ORDER BY s.stop_id ASC
/*THE SAME THING WITH IMPLICIT JOINS*/
SELECT stops.name, stops.stop_id, stop_times.trip_id, stop_times.departure_time, trips.route_id
FROM stops AS s, routes AS r, stop_times AS st, trips AS t
WHERE s.col_name = r.col_name /*JOIN of stops and trips*/
AND st.stop_id = s.stop_id /*JOIN of stop_times and stops*/
AND t.route_id = r.route_id /*JOIN of trips and rutes*/
AND t.route_id = 2 /*Other condition*/
AND st.trip_id = 'SCHED255' /*Other condition*/
ORDER BY s.stop_id ASC
在
FROM
-子句中有4个表。因此,您至少需要3个连接
,以避免数据的多次出现
基本规则:n个表=(n-1)连接
+++++++++++编辑+++++++++++
在您的情况下,它应该如下所示:
/*WITH EXCPLICIT JOINS*/
SELECT stops.name, stops.stop_id, stop_times.trip_id, stop_times.departure_time, trips.route_id
FROM stops s JOIN routes r ON s.col_name = r.col_name /*col_name is the name of the column that is defined as Contraint between these two*/
JOIN stop_times st ON st.stop_id = s.stop_id
JOIN trips t ON t.route_id = r.route_id
AND t.route_id = 2
AND st.trip_id = 'SCHED255'
ORDER BY s.stop_id ASC
/*THE SAME THING WITH IMPLICIT JOINS*/
SELECT stops.name, stops.stop_id, stop_times.trip_id, stop_times.departure_time, trips.route_id
FROM stops AS s, routes AS r, stop_times AS st, trips AS t
WHERE s.col_name = r.col_name /*JOIN of stops and trips*/
AND st.stop_id = s.stop_id /*JOIN of stop_times and stops*/
AND t.route_id = r.route_id /*JOIN of trips and rutes*/
AND t.route_id = 2 /*Other condition*/
AND st.trip_id = 'SCHED255' /*Other condition*/
ORDER BY s.stop_id ASC
带有连接的查询可能如下所示
SELECT s.name
, s.stop_id
, st.trip_id
, st.departure_time
, t.route_id
FROM stops s
JOIN routes r
ON r.some_column = something
JOIN stop_times st
ON st.stop_id = s.stop_id
JOIN trips t
ON t.some_column = some_other_table.some_column
WHERE t.route_id = 2
AND st.trip_id = 'SCHED255'
ORDER
BY s.stop_id ASC
带有连接的查询可能如下所示
SELECT s.name
, s.stop_id
, st.trip_id
, st.departure_time
, t.route_id
FROM stops s
JOIN routes r
ON r.some_column = something
JOIN stop_times st
ON st.stop_id = s.stop_id
JOIN trips t
ON t.some_column = some_other_table.some_column
WHERE t.route_id = 2
AND st.trip_id = 'SCHED255'
ORDER
BY s.stop_id ASC
带有连接的查询可能如下所示
SELECT s.name
, s.stop_id
, st.trip_id
, st.departure_time
, t.route_id
FROM stops s
JOIN routes r
ON r.some_column = something
JOIN stop_times st
ON st.stop_id = s.stop_id
JOIN trips t
ON t.some_column = some_other_table.some_column
WHERE t.route_id = 2
AND st.trip_id = 'SCHED255'
ORDER
BY s.stop_id ASC
带有连接的查询可能如下所示
SELECT s.name
, s.stop_id
, st.trip_id
, st.departure_time
, t.route_id
FROM stops s
JOIN routes r
ON r.some_column = something
JOIN stop_times st
ON st.stop_id = s.stop_id
JOIN trips t
ON t.some_column = some_other_table.some_column
WHERE t.route_id = 2
AND st.trip_id = 'SCHED255'
ORDER
BY s.stop_id ASC
您缺少几个连接条件。如果有查询,您将使用什么php输出?@Mureinik the WHERE子句不充当连接?@davidelmonte您可以在那里设置连接条件,但不建议这样做(这些称为隐式连接)。但是,您缺少处理
stop_times
和trips
表的连接条件。而且您也没有路由条件。您缺少几个连接条件。如果有的话,您使用什么php输出查询?@Mureinik the WHERE子句不充当连接?@davidelmonte您可以在那里拥有连接条件,尽管不推荐(这些称为隐式连接)。但是,您缺少处理stop_times
和trips
表的连接条件。而且您也没有路由条件。您缺少几个连接条件。如果有的话,您使用什么php输出查询?@Mureinik the WHERE子句不充当连接?@davidelmonte您可以在那里拥有连接条件,尽管不推荐(这些称为隐式连接)。但是,您缺少处理stop_times
和trips
表的连接条件。而且您也没有路由条件。您缺少几个连接条件。如果有的话,您使用什么php输出查询?@Mureinik the WHERE子句不充当连接?@davidelmonte您可以在那里拥有连接条件,尽管不推荐(这些称为隐式连接)。但是,您缺少处理stop_times
和trips
表的连接条件。而且您也没有路由条件。谢谢。。我不需要别人来编写我的代码,但是在我的上下文中使用一个连接的示例会非常有用,因为我不知道您的数据模型。所以我不能给你一个确切的解决方案。您能提供一些关于主键和外键约束的信息吗?谢谢。。我不需要别人来编写我的代码,但是在我的上下文中使用一个连接的示例会非常有用,因为我不知道您的数据模型。所以我不能给你一个确切的解决方案。您能提供一些关于主键和外键约束的信息吗?谢谢。。我不需要别人来编写我的代码,但是在我的上下文中使用一个连接的示例会非常有用,因为我不知道您的数据模型。所以我不能给你一个确切的解决方案。您能提供一些关于主键和外键约束的信息吗?谢谢。。我不需要别人来编写我的代码,但是在我的上下文中使用一个连接的示例会非常有用,因为我不知道您的数据模型。所以我不能给你一个确切的解决方案。你能提供一些关于主键和外键约束的信息吗?