gpt4 book ai didi

mysql - 如何从表mysql中取出8个不同的行?

转载 作者:行者123 更新时间:2023-12-01 00:47:07 24 4
gpt4 key购买 nike

我有一个包含两列和很多行的表格

   | id | type      |
|----------------|
| 1 | sweater |
| 2 | jeans |
| 3 | pants |
| 4 | trousers |
| 5 | T-shirt |
| 6 | socks |
| 7 | polo |
| 8 | shirt |
| 9 | sweater |
| 10 | jeans |
| 11 | T-shirt |
..................
and so oooon.......

我需要从表格中取出 8 行不同的类型:帽子、围巾、耳环、项链、戒指、钱包、手套和腰带

我从来不需要这样的查询,我已经试过了。

    SELECT src, 
type
FROM accs_w
WHERE TYPE = 'hat'
OR TYPE = 'earnings'
OR TYPE = 'purse'
OR TYPE = 'scarf'
OR TYPE = 'necklace'
OR TYPE = 'ring'
OR TYPE = 'belt'
OR TYPE = 'gloves'
LIMIT 8

但是,当然它没有返回任何东西

非常感谢您的回答,谢谢!

最佳答案

尝试使用 IN() 子句:

SELECT src, type FROM accs_w WHERE TYPE IN (
'hat', 'earnings', 'purse', 'scarf', 'necklace', 'ring', 'belt', 'gloves'
)

您不需要LIMIT 输出,因为您知道您只会得到 8 行(在这种特定情况下)。

编辑:

OP 似乎需要为他的子集中定义的每个 TYPE 获取一个随机的 SRC。这是如何完成的。

Full SQLFiddle here

select a.type, (
select b.src
from accs_w b
where b.type = a.type
order by rand()
limit 1
)
from (
select 'T-shirt' as type union all
select 'jeans' as type union all
select 'pants' as type
) as a;

它不漂亮,而且 - 我认为 - 性能不佳,但它完成了工作。

关于mysql - 如何从表mysql中取出8个不同的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20789076/

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