gpt4 book ai didi

mysql - 选择 id 不不同的每一行

转载 作者:太空宇宙 更新时间:2023-11-03 10:47:07 26 4
gpt4 key购买 nike

我有点困惑,我正在向一些大师寻求帮助

我有这张表(名为“mytable”)

ID   |   Colum_a   |   Column_b
121 100 NULL
122 150 200
123 100 100
124 50 NULL
125 NULL 5

为了选择 id,我使用了一个像这样的简单查询

SELECT id FROM mytable WHERE Colum_a > 0 OR Column_b > 0

显然,我得到了这个结果:

ID
121
122
123
124
125

现在,我的“头疼”(我很困惑……)。我需要“分隔”两列(Column_a 和 Column_b)以允许“复制”ID。

为了更清楚,我需要这样的结果(在未来的 foreach 循环中使用,php):

ID   |   Result   
121 100
122 150
122 200
123 100
123 100
124 50
125 5

我试过这个查询,但它不起作用...

SELECT ID FROM mytable WHERE Column_a > 0 OR Column_b > 0 IN
(SELECT ID FROM mytable GROUP BY ID HAVING COUNT(*) > 1)

任何帮助将不胜感激。谢谢。

最佳答案

也许使用union all

select ID, Colum_a  as res from mytable WHERE Column_a > 0
union all
select ID, Colum_b as res from mytable WHERE Column_b > 0
order by ID asc;

关于mysql - 选择 id 不不同的每一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30047985/

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