2017-07-31 22 views
2

请看图片。与表“participants‘执行时为什么mysql中的这个查询可以正常工作,但不能与该表的视图匹配?

下面的查询的伟大工程:

SELECT 
    * 
FROM 
    (SELECT 
     p.*, @curRow:[email protected] + 1 AS position 
    FROM 
     (SELECT @curRow:=0) r, 
     participants p 
    WHERE 
     (p.isVolunteer = 0 
      OR p.isVolunteer IS NULL) 
    GROUP BY p.id 
    ORDER BY p.lastRegistrationDate DESC 
    LIMIT 0 , 25) AS p 
     INNER JOIN 
    participants_registrations pr ON p.id = pr.participantId 
     INNER JOIN 
    registrations r ON pr.registrationId = r.id 
ORDER BY p.position ASC , r.createdOn DESC 

但是,当我更换’participants”通过视图“vparticipants”我不能得到相同的结果,那就是suposed应该是一样的。

问题是结果的顺序是不同的。

使用该表,我得到了createdOn列(日期)和position列中值从1到25开始排序的行,但使用视图'vparticipants'我得到的列'position'从6开始到30,并且列创建的顺序方向顺序。我需要与表格相同的结果。

的观点很简单,在这里:

CREATE 
    ALGORITHM = UNDEFINED 
    DEFINER = `root`@`localhost` 
    SQL SECURITY DEFINER 
VIEW `vparticipants` AS 
    SELECT 
     `participants`.`id` AS `id`, 
     `participants`.`identificationNumber` AS `identificationNumber`, 
     `participants`.`firstName` AS `firstName`, 
     `participants`.`lastName` AS `lastName`, 
     `participants`.`email` AS `email`, 
     `participants`.`cellphone` AS `cellphone`, 
     `participants`.`city` AS `city`, 
     `participants`.`gender` AS `gender`, 
     `participants`.`shirtSize` AS `shirtSize`, 
     `participants`.`bloodType` AS `bloodType`, 
     `participants`.`emergencyContactName` AS `emergencyContactName`, 
     `participants`.`emergencyContactPhone` AS `emergencyContactPhone`, 
     `participants`.`eps` AS `eps`, 
     `participants`.`birthday` AS `birthday`, 
     `participants`.`memberOfGroupId` AS `memberOfGroupId`, 
     `participants`.`memberOfGroupName` AS `memberOfGroupName`, 
     `participants`.`lastRegistrationDate` AS `lastRegistrationDate`, 
     `participants`.`isVolunteer` AS `isVolunteer`, 
     `agreements_signatures`.`signatureRequest` AS `signatureRequest`, 
     `agreements_signatures`.`signature` AS `signature`, 
     `agreements_signatures`.`manualSignature` AS `manualSignature`, 
     `agreements_signatures`.`fromWhereWasSigned` AS `fromWhereWasSigned`, 
     `agreements_signatures`.`responsibleName` AS `responsibleName`, 
     `agreements_signatures`.`responsibleIdentificationNumber` AS `responsibleIdentificationNumber`, 
     `agreements_signatures`.`expeditionPlace` AS `expeditionPlace`, 
     `agreements_signatures`.`signedIn` AS `signedIn` 
    FROM 
     ((`participants` 
     LEFT JOIN `participants_registrations` ON ((`participants_registrations`.`participantId` = `participants`.`id`))) 
     LEFT JOIN `agreements_signatures` ON ((`agreements_signatures`.`id` = `participants_registrations`.`agreementSignatureId`))) 

感谢任何帮助。

正确的结果IMAGE: enter image description here

错误的结果IMAGE(VIEW): enter image description here

+0

对于使用视图是必要的,因为它提供了协议签名表列 –

+0

您有一个GROUP BY子句,但没有聚合函数。通常情况下,这将返回广泛不确定的结果,但可能会受到可能存在的任何索引的影响。在MySQL中,视图(我认为)没有索引的意识,所以我怀疑这可能会影响结果。 – Strawberry

回答

0

它,因为ORDER BY不与视图的工作,看到this链接,其SQL SERVER的观点的限制。不使用视图编写此查询会更好

+0

这是mysql,我删除了命令,并不起作用。 –

+0

您应该使用直接查询或将所有逻辑放入视图本身 –

+0

在您的视图中您已经加入了两个以上的表格,当然,它们也会在查看视图时影响结果,只需删除那些左连接,然后再次尝试if结果是好的 –

相关问题