简体   繁体   English

无法弄清楚如何编写此查询

[英]Can't figure out how to write this query

I have a couple hundred records in a table and I need to get the the last 50 of them, but I need 25 males and 25 females. 我的桌子上有几百条记录,我需要获取其中的最后50条记录,但我需要25位男性和25位女性。 There is a gender column. 有一个性别列。 I just can't figure out how to make that query. 我只是不知道如何进行查询。 Can I get some help? 我可以帮忙吗?

If you need exactly 50, use a UNION which gets the last 25 of each. 如果您恰好需要50个,请使用UNION来获取每个的最后25个。 This assumes you have some auto-incrementing id column which is sorted in descending order to retrieve the newest 25 of each gender. 假设您有一些自动递增的id列,该列以降序排序以检索每个性别的最新25个字符。

In order to use an ORDER BY and LIMIT for individual components of a UNION , MySQL expects those components to be enclosed in () . 为了对UNION各个组件使用ORDER BYLIMIT ,MySQL希望这些组件包含在()

(SELECT * FROM tbl WHERE gender='F' ORDER BY id DESC LIMIT 25)
UNION ALL
(SELECT * FROM tbl WHERE gender='M' ORDER BY id DESC LIMIT 25)

Note : By the way, I've used SELECT * here for brevity but you should never actually do that in a UNION query. 注意 :顺便说一句,为了简洁起见,我在这里使用了SELECT * ,但是您绝对不应在UNION查询中这样做。 Instead, explicitly list the columns you need, and make sure they're in the same order in both parts of the UNION . 相反,显式列出所需的列,并确保在UNION两个部分中它们的顺序相同。

Like: 喜欢:

(SELECT col1, col2, coln FROM tbl WHERE gender='F'....)
UNION ALL
(SELECT col1, col2, coln FROM tbl WHERE gender='M'....)
(SELECT * FROM table WHERE gender = 'M' ORDER BY id DESC LIMIT 25)
UNION
(SELECT * FROM table WHERE gender = 'F' ORDER BY id DESC LIMIT 25)

That would work I guess. 我猜那行得通。 For the order by ID part I assumed you have an autoincrement id column, making the last added records the ones with the highest ID. 对于按ID排序的订单,我假设您有一个自动递增ID列,使最后添加的记录记录ID最高的列。

I just thought of this quickly, no guarantees. 我只是很快想到了这一点,没有保证。

SELECT * FROM table WHERE gender = 'male' ORDER BY ID DESC LIMIT 25
UNION ALL
SELECT * FROM table WHERE gender = 'female' ORDER BY ID DESC LIMIT 25

Basically, it gets the last 25 men, and the last 25 women, and combines the results together. 基本上,它将获得最后25位男性和最后25位女性,并将结果合并在一起。

there is no such concept of LAST unless you have another column which you can use to ORDER BY . 除非您还有可以用于ORDER BY列,否则没有LAST的概念。

i suggest write 2 queries, on for males only, and another for females only. 我建议写两个查询,仅针对男性,另一个仅针对女性。

then use an order by so there is a 'last' set. 然后使用order by,这样就设置了“最后一个”。

then flip that (using DESC ) so they are FIRST instead. 然后翻转(使用DESC ),使其成为FIRST。

then use LIMIT clause to select the number you want. 然后使用LIMIT子句选择所需的号码。

then finally, UNION them together. 然后最后, UNION在一起。

Try using two queries, one to grab the 25 latest males and a second to grab the 25 latest females. 尝试使用两个查询,一个查询获取25个最新的男性,另一个查询获取25个最新的女性。 Then just concatenate the results. 然后将结果串联起来。

SELECT * FROM males LIMIT 25 ORDER BY DESC

SELECT * FROM females LIMIT 25 ORDER BY DESC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM