在使用 SQL 查询时,如果查询结果中出现了多个相同的数据,可能是由于连接的表中存在多个匹配的记录导致的。为了规避这个问题,可以使用 DISTINCT 关键字来去除重复的记录。

原始查询语句:

SELECT fo.oid,f.flight_num,f.flight_date,f.start_city,f.end_city,p.pname,fo.order_amount,fo.order_status,fo.order_time FROM `flightorder` fo,passenger p,flight f WHERE  p.username=fo.username AND f.fid=fo.fid

改进后的查询语句:

SELECT DISTINCT fo.oid, f.flight_num, f.flight_date, f.start_city, f.end_city, p.pname, fo.order_amount, fo.order_status, fo.order_time 
FROM `flightorder` fo, passenger p, flight f 
WHERE p.username=fo.username AND f.fid=fo.fid

在上述查询语句中,使用了 DISTINCT 关键字来确保查询结果中不会出现重复的记录。

SQL 查询结果出现重复数据的原因及解决方法

原文地址: https://www.cveoy.top/t/topic/qhxU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录