繁体   English   中英

每个条件的不同输出。按所有值组合在一起-单个mysql查询

[英]Different outputs for each condition.get together group by all values - single mysql query

这是我的表格的样子:

CREATE TABLE my_table(id INT,project_id VARCHAR(6),order_id VARCHAR(6),user_id VARCHAR(6),owner_id VARCHAR(6));

INSERT INTO my_table
VALUES
  (1, 211541,   8614,   1605,   0),
  (2, 211541,   8614,   16079,  1605),
  (3, 210446,   0,      12312,  0),
  (4, 208216,   0,      16467,  14499),
  (5, 208216,   0,      14499,  0),
  (6, 208216,   0,      14499,  0),
  (7, 208216,   0,      16467,  14499),
  (8, 209377,   0,      7556,   0),
  (9, 209324,   0,      7556,   0),
  (10,201038,   8602,   9390,   101);

我必须检查拆分多个条件:
查询执行是这种方式。

order_id!= 0
最初转到project_id,
(即)
1.project_id-211541然后第一个条件(owner_id = 0),选择user_id
注意:
-如果未获得user_id(空结果)-转到第二个条件。
-如果获取user_id-不要进入第二种情况。


2.project_id-211541-第二个条件(owner_id!= 0),选择owner_id。
我有
my_user_id
1605
101

order_id = 0

(即)1.project_id-208216然后是第一个条件(owner_id = 0),按user_id选择分组
注意:
-如果未获得user_id(空结果)-转到第二个条件。
-如果获取user_id-不要进入第二种情况。


2.project_id-208216-第二个条件(owner_id!= 0),按owner_id选择分组。
我有
my_user_id
12312
14499
7556
7556

最后,我需要这个答案-按my_user_id分组
my_user_id
1605
101
12312
14499
7556

注意:

我需要一个查询。

为什么不仅仅使用IF?

SELECT 
    IF (order_id = 0, user_id, owner_id) AS new_val
FROM my_table
GROUP BY new_val

当更多地查看它时,您似乎需要更多的ifs ..这样的东西?

SELECT 
    if(order_id <> 0,
        if(owner_id = 0, user_id, owner_id), 
        if(user_id = 0, owner_id, user_id)
    ) AS new_val
FROM my_table
group by new_val

这是我从您的情况中了解到的

我给他们编号,然后把它们放在if条件下

if the order_id is not 0,  -- 1
check to see if the owner_id is 0,

if owner_id = 0 -- 2
then pull in user_id -- 3

else owner_id is not 0  
and you pull in owner_id -- 4

写这样更像代码

if(order_id <> 0, if(owner_id <> 0, owner_id, user_id), some condition for when order_id is 0)

其他情况

if the order_id is 0
pull in user_id (grouped) 

if user_id = 0 -- 5
then pull in owner_id -- 6

else user_id is not 0
and pull in user_id -- 7

将其与另一部分替换为0时的其他条件。

--      1              2              3          4           5             6         7

if(order_id <> 0, if(owner_id = 0, user_id, owner_id), if(user_id = 0, owner_id, user_id))

现在对其进行格式化,以便其可读

if(order_id <> 0, -- if its not 0
    if(owner_id = 0, user_id, owner_id), -- true condition
    if(user_id = 0, owner_id, user_id) -- false condition
)

我对么?

您可以使用'if'。 例如

@result=if(1!=2,'yes','no');

这将使结果的值为“是”。 这些可以嵌套以创建复杂的条件:

SET @value_a='no';
set @value_b='';
set @value_c=’test’;
SET @value_d=’’;


set @result=
(
select if(@value_a!=’no’,(SELECT column1 FROM table1 WHERE id= @value_a),
            if(@value_b!='',@value_b,
                if(@value_c!='',(select column2 from table2 where id=@value_a),
                    if(@value_d!='',(select  column3 from table3 where id=@value_a),
                        ‘I am a default value’
                    )
                )
            )
        )
);

给出table2中id = @ value_a给出的所有选择列2。

暂无
暂无

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

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