-1

我有两个表存储有关上传到站点调用图像和审核的图像的信息。上传图像时,它的路径存储在 image.imageLocation 中,当它用于审计时,auditID 保存到同一个表中。auditID 是用于将表连接在一起的值。

我正在尝试进行查询,该查询将使用审核的日期(即 audit.dateStart)告诉我使用该图像的最新审核。

到目前为止我的查询是......

SELECT * FROM (
    SELECT image.imageID AS newest_image, image.userID AS image_userID,
           image.auditID as image_auditID, image.type AS image_type,
           image.imageLocation AS location
    from image
) image
LEFT JOIN audit ON image_auditID = audit.auditID
WHERE image_userID = '$id' AND image_type = 'standard'
      AND dateStart = MAX(dateStart)
GROUP by location

这会引发错误,说明对 GROUP 的使用不当。

任何帮助将不胜感激

这是表结构

audit

auditID     varchar(255)        
userID      int(11)     
dateStart       date        
dateEnd     date    
auditName       varchar     
auditDesc       varchar     
imagesUploaded  tinyint 
actionPlan      varchar     
auditComplete   tinyint 
dateComplete    datetime

image

imageID         int(10)     
name            varchar(255)        
auditID         varchar(255)        
imageGrade          int(1)      
exposure            varchar(255)        
positioning         varchar(255)        
patient         varchar(255)        
equipment           varchar(255)        
positioningReason   varchar(255)        
exposureReason  varchar(255)        
patientReason   varchar(255)        
comment         varchar(255)        
auditDate           datetime    
imageLocation   varchar(255)        
thumbnail           text        
xrayType            varchar(10)     
type            varchar(255)        
userID          int(11) 

Some sample data

imageID = 563
name = standard_16.jpg
auditID = 1360318160843025059
imageLocation = upload/2/standards/standard_16.jpg
type = standard
userID = 2

imageID = 565
name = standard_18.jpg
auditID = 1360318160843025059
imageLocation = upload/2/standards/standard_18.jpg
type = standard
userID = 2

imageID = 566
name = standard_18.jpg
auditID = 13603248241777655575
imageLocation = upload/2/standards/standard_18.jpg
type = standard
userID = 2

auditID = 1360318160843025059
userID = 2
dateStart = 2013-02-14

auditID = 13603248241777655575
userID = 2
dateStart = 2013-02-09

如您所见,相同的图像可用于多个审核,我想知道哪个审核最后使用了该图像,在这种情况下,结果将是 auditID 1360318160843025059

4

2 回答 2

1

您是否尝试过使用子查询。此子查询将获取max(dateStart)每个审计的。然后,您将此结果连接到audit表中,以返回具有最大日期的行的所有详细信息:

SELECT 
  i.imageID AS newest_image, 
  i.userID AS image_userID, 
  i.auditID as image_auditID, 
  i.type AS image_type, 
  i.imageLocation AS location,
  a1.*
FROM image i
LEFT JOIN audit a1
  on i.auditID = a1.auditID
INNER JOIN 
(
  select auditID, max(dateStart) MaxDate
  from audit
  group by auditID
) a2
  on a1.auditID = a2.auditID
  and a1.dateStart = a2.MaxDate
WHERE i.userID = '$id' 
  AND i.type = 'standard' 

编辑,如果您只有最后的图像/审核,那么您可以使用:

SELECT i.imageID AS newest_image, 
  i.userID AS image_userID, 
  i.auditID as image_auditID, 
  i.type AS image_type, 
  i.imageLocation AS location,
  a1.*
FROM image i
LEFT JOIN audit a1
  on i.auditID = a1.auditID
INNER JOIN 
(
  select max(dateStart) MaxDate
  from audit
) a2
  on a1.dateStart = a2.MaxDate
WHERE i.type = 'standard';

请参阅带有演示的 SQL Fiddle


只是另一个猜测:

SELECT i.imageID AS newest_image, 
  i.userID AS image_userID, 
  i.auditID AS image_auditID, 
  i.type AS image_type, 
  i.imageLocation AS location,
  a.*
FROM image i
  JOIN audit a
    ON i.auditID = a.auditID
  JOIN 
    (
    SELECT i.imageLocation, MAX(a.dateStart) MaxDate
    FROM image i
      JOIN audit a
        ON i.auditID = a.auditID
    WHERE i.userID = '$id' 
      AND i.type = 'standard' 
    GROUP BY i.imageLocation 
    ) g
    ON  g.MaxDate = a.dateStart
    AND g.imageLocation = i.imageLocation
WHERE i.userID = '$id' 
  AND i.type = 'standard' 
于 2013-02-08T17:34:06.637 回答
1

试试这个

 SELECT * FROM (
SELECT image.imageID AS newest_image, image.userID AS image_userID,
       image.auditID as image_auditID, image.type AS image_type,
       image.imageLocation AS location
 from image
 GROUP by location
              ) image
LEFT JOIN audit ON image_auditID = audit.auditID
WHERE image_userID = '$id' AND image_type = 'standard'
  AND dateStart = MAX(dateStart)

编辑。

 SELECT image.imageID AS newest_image, image.userID AS image_userID,
   image.auditID as image_auditID, image.type AS image_type,
   image.imageLocation AS location
   from image
  LEFT JOIN audit ON image.userID = audit.auditID
  INNER JOIN 
 (
   select max(dateStart) MaxDate
   from audit
  ) audit2
  on audit.dateStart = audit2.MaxDate
  WHERE  image.type = 'standard'
  GROUP by image.imageLocation

SQL 小提琴演示

于 2013-02-08T17:38:11.877 回答