mysqli通过另一列选择不同的顺序

vltsax25  于 2021-06-18  发布在  Mysql
关注(0)|答案(3)|浏览(332)

我有以下mysql数据库:

variable    displayname    clevs      ccols    cmin    cmax  show_on_quicklinks  client
'capesfc'   'cape'         '100 200'  '18 19'  '100'   '3400' 'gfs,access-c'     NULL
'lftxsfc'   'li'           '-10 -9'   '50 52'  '-10'   '0'    'gfs'              NULL
'lftxsfc'   'li'           '-10 -9'   '50 52'  '-10'   '0'    'gfs,access-c'     'client1'
'tscreen'   'tempsfc'      '-10 45'   '50 52'  '-10'   '45'   'gfs,access-c'     'client1'
'lftxsfc'   'li'           '-10 -9'   '50 52'  '-10'   '0'    'access-c'         'client2'
'capesfc1'  'cape1'        '100 200'  '18 19'  '100'   '3400' 'gfs,access-c'     NULL

我想选择客户端不为空的唯一displayname,否则只返回displayname。。。因此,在下面的示例中,如果选择client='client1',它将返回client1的行,并且还必须返回client为null的所有其他行。它不能为其他客户端返回任何其他行:

variable   displayname  clevs      ccols    cmin    cmax  show_on_quicklinks    client
'lftxsfc'  'li'         '-10 -9'   '50 52'  '-10'   '0'    'gfs,access-c'       'client1'
'tscreen'  'tempsfc'    '-10 45'   '50 52'  '-10'   '45'   'gfs,access-c'       'client1'
'capesfc'  'cape'       '100 200'  '18 19'  '100'   '3400' 'gfs,access-c'        NULL
'capesfc1' 'cape1'      '100 200'  '18 19'  '100'   '3400' 'gfs,access-c'        NULL
e4yzc0pl

e4yzc0pl1#

以下是您在rextester.com/uijk63292上查询的变体

SELECT
     a_index
    ,client 
    ,variable
    ,displayname
    ,color_info
    ,clevs
    ,ccols
    ,cmin
    ,cmax
    ,cint
    ,show_on_models_quicklinks
FROM `variables_info` 
WHERE `client` = 'client1' 
OR (`client` IS NULL 
    and `displayname` IN (SELECT `displayname` FROM `variables_info` 
                          WHERE `client` IS NULL 
                          and NOT `displayname` IN (SELECT `displayname` 
                                                    FROM `variables_info` 
                                                    WHERE `client` = 'client1')
                         )
   )
;

注意:我猜您只需要client1中尚未包含的任何displayname,它由以下内容返回:

SELECT `displayname` FROM `variables_info` 
WHERE `client` IS NULL 
and NOT `displayname` IN (SELECT `displayname` 
                          FROM `variables_info` 
                          WHERE `client` = 'client1')

看到了吗http://rextester.com/llokz9426

xpszyzbs

xpszyzbs2#

您可以尝试使用聚合来执行以下操作,但是如果您使用的是mysql的升级版本,那么您可以使用row\ u number()函数,这样更容易识别重复项

select variable,displayname,clevs,ccols,cmin,cmax,max(show_on_quicklinks) as show_on_quicklinks,max(client) as client
    from tablename
    gruop by variable,displayname,clevs,ccols,cmin,cmax
kcugc4gi

kcugc4gi3#

更改样本后修改查询:

set @client := 'client1';
set @var := (select variable from variables_info where client = @client limit 1);

SELECT
      variable,displayname,clevs,ccols,cmin,cmax,show_on_quicklinks,client
FROM (
    SELECT
          @row_num :=IF(@prev_value = concat(v.variable, coalesce(v.client,'')), @row_num + 1, 1)AS RowNumber
        , v.*
        , @prev_value := concat(v.variable, coalesce(v.client,''))
    FROM variables_info AS v
    CROSS JOIN (SELECT @row_num :=1,  @prev_value :='') vars
    ORDER BY 
        v.variable, v.client
    ) AS d
WHERE (
       (RowNumber = 1 and client = @client)
    OR (RowNumber = 1 and client IS NULL and variable <> @var)
     )

另请参见:http://rextester.com/vhndh48312
注意:上面的查询模拟了“row\u number()”,这是一个非常有用的“窗口函数”,可以在mysql 8和许多其他sql数据库中找到。
原始:
这不是一个有效的查询,但是它从您的示例3中找到了想要的2行。

select

* 

from variables_info
where (variable in (select variable from variables_info where client = 'client1') and client is not null
     OR
       variable NOT in (select variable from variables_info where client = 'client1') and client is null
      )

请参见:http://rextester.com/taym62841
如果你有一个mysql的最新版本,那么更好的方法是可用的。

相关问题