mysql限制无法针对1个id工作多个记录

时间:2018-09-03 11:29:20

标签: mysql database stored-procedures limit

Mysql过程针对1个id给出三个记录。我正在尝试仅针对1个ID获得1条记录。限制访问1条ID的1条记录

创建DEFINER = xxxx @ xxxx.%程序xxxx

    IN pCompanyId BIGINT(20)
        )
    BEGIN
        SELECT 
         `zp`.`PlaceId`,
         `zp`.`PlaceName`,
         `zp`.`PlaceCategoryCode`,
         `zp`.`Description`,
         `zp`.`CompanyId_FK`,
         `zp`.`OwnerCompanyId_FK`,
         `zp`.`IsDeleted`,
         `zp`.`IsArchived`,
         `zp`.`CreatedDate`,
         `zp`.`ModifiedDate`,
         `zp`.`CreatedBy_FK`,
         `zp`.`ModifiedBy_FK`,
         (SELECT COUNT(`ApplianceId`) FROM `ZThinQ_Appliance` WHERE `ApplianceActionCode` = 1 AND `PlaceId_FK` = `PlaceId`)  AS activeDevices ,
         (SELECT COUNT(`ApplianceId`) FROM `ZThinQ_Appliance` where  `PlaceId_FK` = `PlaceId` ) AS totalDevices

        FROM `ZThinQ_Place` AS `zp` 
        JOIN `ZThinQ_Appliance` as `za`

        #ON `ZThinQ_Appliance`
        #ON `PlaceId` = `PlaceId_FK` 

        Where `zp`.`CompanyId_FK`=pCompanyId;


    END

2 个答案:

答案 0 :(得分:0)

如果要行,可以将限制1添加到yoru查询

 SELECT 
     `zp`.`PlaceId`,
     `zp`.`PlaceName`,
     `zp`.`PlaceCategoryCode`,
     `zp`.`Description`,
     `zp`.`CompanyId_FK`,
     `zp`.`OwnerCompanyId_FK`,
     `zp`.`IsDeleted`,
     `zp`.`IsArchived`,
     `zp`.`CreatedDate`,
     `zp`.`ModifiedDate`,
     `zp`.`CreatedBy_FK`,
     `zp`.`ModifiedBy_FK`,
     (SELECT COUNT(`ApplianceId`) FROM `ZThinQ_Appliance` WHERE `ApplianceActionCode` = 1 AND `PlaceId_FK` = `PlaceId`)  AS activeDevices ,
     (SELECT COUNT(`ApplianceId`) FROM `ZThinQ_Appliance` where  `PlaceId_FK` = `PlaceId` ) AS totalDevices

    FROM `ZThinQ_Place` AS `zp` 
    JOIN `ZThinQ_Appliance` as `za`

    #ON `ZThinQ_Appliance`
    #ON `PlaceId` = `PlaceId_FK` 

    Where `zp`.`CompanyId_FK`=pCompanyId
    LIMIT 1 

答案 1 :(得分:0)

我有1条ID的多条记录。所以我在程序中使用select different。就是这样

SELECT distinct
 `zp`.`PlaceId`,
 `zp`.`PlaceName`,
 `zp`.`PlaceCategoryCode`,
 `zp`.`Description`,
 `zp`.`CompanyId_FK`,
 `zp`.`OwnerCompanyId_FK`,
 `zp`.`IsDeleted`,
 `zp`.`IsArchived`,
 `zp`.`CreatedDate`,
 `zp`.`ModifiedDate`,
 `zp`.`CreatedBy_FK`,
 `zp`.`ModifiedBy_FK`,
 (SELECT COUNT(`ApplianceId`) FROM `ZThinQ_Appliance` WHERE `ApplianceActionCode` = 1 AND `PlaceId_FK` = `PlaceId`)  AS activeDevices ,
 (SELECT COUNT(`ApplianceId`) FROM `ZThinQ_Appliance` where  `PlaceId_FK` = `PlaceId` ) AS totalDevices

FROM `ZThinQ_Place` AS `zp` 
JOIN `ZThinQ_Appliance` as `za`

#ON `ZThinQ_Appliance`
#ON `PlaceId` = `PlaceId_FK` 

Where `zp`.`CompanyId_FK`=pCompanyId
LIMIT 1