MySQL Query返回意外结果

时间:2017-03-20 19:41:10

标签: mysql

我的Preneed表只包含3行;

Preneed_tb
+----+-----------+----------+---------+----------+
| id | fullname  | plannum  | remarks | pre_type |
+----+-----------+----------+---------+----------+
|  1 | jAMES YAP | SDF12321 | FB      | HIP      |
|  2 | asdasdas  | 213      | 1gb     | CEA      |
|  3 | gdaae     | 12fvs    | dfcv    | CEA      |
+----+-----------+----------+---------+----------+

MySQL Query返回具有相同数据的重复行:

select    fullname as 'FullName', 
          plannum as 'Plan_Number', 
          remarks as 'Remarks', 
          pre_type as 'Pre_Need_Type', 
          concat(x.id,'-PRENEED') as 'Identification' 
from      preneed_tb as x
left join filelocation 
on        filelocation.f_id = x.id
left join (select   max(f_logs.id),
                    f_id, 
                    log_status 
           from     f_logs 
           group by f_id) as y 
on         y.f_id = x.id
WHERE      concat(f_location,' ') like 'SFDSF %' 
and        concat(y.log_status, ' ') like 'IN STORA%'

我使用x.id是因为我得到了 我试图使用preneed_tb时,'字段列表'中的未知列'preneed_tb.id'

示例结果:

+-----------+-------------+---------+---------------+----------------+
| FullName  | Plan_Number | Remarks | Pre_Need_Type | Identification |
+-----------+-------------+---------+---------------+----------------+
| jAMES YAP | SDF12321    | FB      | HIP           | 1-PRENEED      |
| jAMES YAP | SDF12321    | FB      | HIP           | 1-PRENEED      |
| jAMES YAP | SDF12321    | FB      | HIP           | 1-PRENEED      |
+-----------+-------------+---------+---------------+----------------+

然后当我使用此查询时,它会返回预期结果:

 select   fullName,
          Plan_Number,
          Remarks, 
          Pre_Need_Type, 
          Identification 
    from     (select fullname as 'FullName', 
                      plannum as 'Plan_Number', 
                     remarks as 'Remarks', 
                     pre_type as 'Pre_Need_Type', 
                     concat(preneed_tb.id,'-PRENEED') as 'Identification' 
              from   preneed_tb) as x
left join filelocation 
on filelocation.f_id = x.identification
left join (select   max(f_logs.id),
                        f_id, 
                        log_status 
               from     f_logs 
               group by f_id) as y 
    on         y.f_id = x.identification
    WHERE      concat(f_location,' ') like 'SFDSF %' 
    and        concat(y.log_status, ' ') like 'IN STORA%'

示例结果:

+-----------+-------------+---------+---------------+----------------+
| FullName  | Plan_Number | Remarks | Pre_Need_Type | Identification |
+-----------+-------------+---------+---------------+----------------+
| jAMES YAP | SDF12321    | FB      | HIP           | 1-PRENEED      |
+-----------+-------------+---------+---------------+----------------+

任何人都可以向我解释这个吗?我很困惑,因为它返回重复的条目,但我的表中只有一个匹配

2 个答案:

答案 0 :(得分:0)

您希望删除重复的行使用distinct子句

  select distinct 
     fullname as 'FullName'
    , plannum as 'Plan_Number'
    , remarks as 'Remarks'
    , pre_type as 'Pre_Need_Type'
    , concat(preneed_tb.id,'-PRENEED') as 'Identification' 
  from preneed_tb as x
  left join filelocation on filelocation.f_id = x.id
  left join (select max(f_logs.id),f_id, log_status 
                from f_logs group by f_id) as y on y.f_id = x.id
  WHERE concat(f_location,' ') like 'SFDSF %' 
  and concat(y.log_status, ' ') like 'IN STORA%'"

答案 1 :(得分:0)

你在第一个条件中已经倒置了ON条件

filelocation.f_id = x.id

其中x = preneed_tb

在第二个你有

filelocation.f_id = x.identification

其中x = preneed_tb