簡體   English   中英

查詢寬度和高度,在同一查詢中每條記錄的大小都超過另一條記錄?

[英]Query for width and height, a record with each greater than the other in the same query?

我正在嘗試創建一個單一查詢,該查詢將從具有以下要求的表中提取結果:

1) Query database table for 5 records.
2) At least one record with image height greater than image width.
3) At least one record with image width greater than image height.
4) Results must be ordered by newest records first (time)

怎么做? 謝謝!

CREATE TABLE `images` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `image` char(100) NOT NULL DEFAULT '',
  `image_width` smallint(4) unsigned NOT NULL DEFAULT '0',
  `image_height` smallint(4) unsigned NOT NULL DEFAULT '0',
  `time` int(11) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
)

編輯 :添加時間字段和時間順序要求。 記錄必須按時間字段排序(對於示例使用任意int字段)。

SELECT * FROM images WHERE id IN (
     SELECT id FROM images WHERE image_width > image_height ORDER BY RAND() LIMIT 1
     UNION
     SELECT id FROM images WHERE image_height > image_width ORDER BY RAND() LIMIT 1
     UNION
     SELECT id FROM images ORDER BY RAND() LIMIT 3
)

擴展其他聯合嘗試...最內部的查詢應該返回7條記錄,即使是隨機的,你仍然可以獲得WIDTH和/或HEIGHT圖像。 因此,通過額外的列進行內部大多數查詢和排序,我添加了“Required”= 1或2將把所需的那些放在第一個和第二個之后。 然后,OUTER查詢將從內部查詢中選擇DISTINCT元素,內部查詢應該以內部查詢結果的自然順序開始1必需,1必需,2必需,2必需,2必需,2必需,2必需。 通過對ID進行區分,如果第一個“2 required”是與寬度或高度相同的ID,則會立即忽略它,將外部自然結果集保留為6條記錄...因此最外層的LIMIT將強制截斷它直到你希望的最后5。

select images.*    
   from images,
        ( select distinct id
             from ( SELECT QryUnions.id, 
                           QryUnions.required
                       FROM ((select imgW.id, 
                                     1 Required,
                                     rand() rand1
                                from images imgW
                                where imgW.image_width > imgW.image_height
                                group by 1
                                order by 3
                                limit 1)
                             union all
                             (select imgH.id, 
                                     1 Required,
                                     rand() rand1
                                from images imgH
                                where imgH.image_height > imgH.image_width
                                group by 1
                                order by 3
                                limit 1)
                             union all
                             (select imgR.id, 
                                    2 Required,
                                    rand() rand1
                                from images imgR
                                group by 1
                                order by 3
                                limit 5)) QryUnions
                        order By
                          QryUnions.Required ) PreQualify
            order by
                Required 
            limit 5 ) Final5
    where
       images.id = Final5.id
SELECT * FROM images WHERE image_width > image_height LIMIT 1
UNION
SELECT * FROM images WHERE image_height > image_width LIMIT 1
UNION
(SELECT * FROM images LIMIT 3);

要避免行重復,您可以使用以下內容:

SELECT DISTINCT * FROM (
    SELECT * FROM images WHERE image_width > image_height LIMIT 1
    UNION
    SELECT * FROM images WHERE image_height > image_width LIMIT 1
    UNION
    (SELECT * FROM images LIMIT 3)
) AS i;
SELECT DISTINCT id FROM (
    SELECT *, 'A' AS ord FROM images WHERE image_width > image_height LIMIT 1
    UNION
    SELECT *, 'B' AS ord FROM images WHERE image_height > image_width LIMIT 1
    UNION
    (SELECT *, 'C' AS ord FROM images)
) AS i ORDER BY ord LIMIT 5;

如果要檢索完整行,可以執行以下操作:

SELECT * FROM images WHERE id IN (
    SELECT id FROM (
        SELECT *, 'A' AS ord FROM images WHERE image_width > image_height LIMIT 1
        UNION
        SELECT *, 'B' AS ord FROM images WHERE image_height > image_width LIMIT 1
        UNION
        (SELECT *, 'C' AS ord FROM images LIMIT 7)
    ) AS i ORDER BY ord
) LIMIT 5;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM