gpt4 book ai didi

sql - 查询宽度和高度,在同一个查询中一个记录大于另一个记录?

转载 作者:可可西里 更新时间:2023-11-01 06:50:34 25 4
gpt4 key购买 nike

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

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
)

关于sql - 查询宽度和高度,在同一个查询中一个记录大于另一个记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5006438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com