MySQL:是否可以返回“混合”数据?数据集?

发布于 2024-08-29 08:50:07 字数 291 浏览 2 评论 0原文

我想知道 MySQL 中是否有一些聪明的方法可以根据特定标准返回“混合/平衡”数据集?

为了说明这一点,假设表中存在类型 1 或类型 2 的潜在结果(即,每条记录的列的值为 1 或 2)。是否有一个聪明的查询能够直接按顺序返回 1 和 2 之间交替的结果:

第一个记录的类型为 1, 第二条记录属于类型 2, 第 3 条记录属于类型 1, 第 4 条记录属于类型 2, 等等...

如果问题很愚蠢,我只是在寻找一些选择。当然,我可以返回任何数据并在 PHP 中执行此操作,但它确实添加了一些代码。

谢谢。

I'm wondering if there's some clever way in MySQL to return a "mixed/balanced" dataset according to a specific criterion?

To illustrate, let's say that there are potential results in a table that can be of Type 1 or Type 2 (i.e. a column has a value 1 or 2 for each record). Is there a clever query that would be able to directly return results alternating between 1 and 2 in sequence:

1st record is of type 1,
2nd record is of type 2,
3rd record is of type 1,
4th record is of type 2,
etc...

Apologies if the question is silly, just looking for some options. Of course, I could return any data and do this in PHP, but it does add some code.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蛮可爱 2024-09-05 08:50:07

像这样的查询应该执行以下操作:

Select some_value, x, c
From
(
  Select
    some_value, x,
    Case When x=1 Then @c1 Else @c2 End As c,
    @c1 := Case When x=1 Then @c1+2 Else @c1 End As c1,
    @c2 := Case When x=2 Then @c2+2 Else @c2 End As c2
  From test_data, (Select @c1:=0, @c2:=1) v
  Order By some_value
) sub
Order By c

它将唯一的偶数分配给 x=0,将奇数分配给 x=1,并使用这些值作为排序条件。


它返回

some_value  x  c
A           1  0
X           2  1
B           1  2
Y           2  3
C           1  4
Z           2  5

以下测试数据:

Create Table test_data (
  some_value VARCHAR(10),
  x  INT
);

Insert Into test_data Values('A', 1);
Insert Into test_data Values('B', 1);
Insert Into test_data Values('C', 1);
Insert Into test_data Values('Z', 2);
Insert Into test_data Values('Y', 2);
Insert Into test_data Values('X', 2);

在交替规则中,值按 some_value 排序,您可以在内部选择中更改此设置,或在其中添加条件。

如果某种类型有更多值(12),您将在其余值之后获取它们(1 2 1 2 2 2) 。

Something like this query should do:

Select some_value, x, c
From
(
  Select
    some_value, x,
    Case When x=1 Then @c1 Else @c2 End As c,
    @c1 := Case When x=1 Then @c1+2 Else @c1 End As c1,
    @c2 := Case When x=2 Then @c2+2 Else @c2 End As c2
  From test_data, (Select @c1:=0, @c2:=1) v
  Order By some_value
) sub
Order By c

It assigns unique even numbers to x=0, and odd numbers to x=1, and uses these values as sort criteria.


It returns

some_value  x  c
A           1  0
X           2  1
B           1  2
Y           2  3
C           1  4
Z           2  5

for the following test-data:

Create Table test_data (
  some_value VARCHAR(10),
  x  INT
);

Insert Into test_data Values('A', 1);
Insert Into test_data Values('B', 1);
Insert Into test_data Values('C', 1);
Insert Into test_data Values('Z', 2);
Insert Into test_data Values('Y', 2);
Insert Into test_data Values('X', 2);

Within the alternating rule values are sorted by some_value, you can change this in the inner select, or add your conditions there.

If there are more values of a certain type (1 or 2), you get them after the rest (1 2 1 2 2 2).

甜心 2024-09-05 08:50:07

您可以使用 IF 函数作为 SELECT 语句的一部分来更改列,但我不确定如何在两列之间自动交替。但是,如果您找到合适的条件,这将适合您

SELECT IF(condition, first_column, second_column) FROM your_table

first_columnsecond_column 可以是不同的类型,例如:

SELECT IF(id > 10, name, status_id) FROM clients

nameVARCHARstatus_idINT

You can use IF function as a part of your SELECT statement to change columns, but I'm not sure how to make is alternate automatically between two columns. If you however find proper condition this will work for you

SELECT IF(condition, first_column, second_column) FROM your_table

first_column and second_column can be of different type, for example:

SELECT IF(id > 10, name, status_id) FROM clients

works well when name is a VARCHAR and status_id is an INT

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文