查询宽度和高度,在同一查询中每条记录的大小都超过另一条记录?

Hig*_*ife 6 mysql sql

我正在尝试创建一个单一查询,该查询将从具有以下要求的表中提取结果:

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)
Run Code Online (Sandbox Code Playgroud)

怎么做?谢谢!

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`)
)
Run Code Online (Sandbox Code Playgroud)

编辑:添加时间字段和时间顺序要求.记录必须按时间字段排序(对于示例使用任意int字段).

Ale*_*dro 0

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);
Run Code Online (Sandbox Code Playgroud)

为了避免行重复,您可以使用以下命令:

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;
Run Code Online (Sandbox Code Playgroud)