MySQL ORDER BY IF () condition sort

source

In doing sqlzoo, encounter a SQL sort of problem, he qualified individual lines can be placed at the beginning of the query results, or the results of the tail

IN method is the statement (also by an IF statement)

 

I'd had a test, as this is the table of contents for all

 

ORDER BY fit using the IF statement

For example, I think the number of rows to the species of snake, listed separately, so I can query

SELECT * FROM pet ORDER BY if (species='snake',0,1),species;

The results are as follows

We can see that the number of lines for the species of snake, was forcibly placed in the beginning of the query results

This is how to do it?

It should be noted:

if (species='snake',0,1),species;

What this means is that while I sort of species, species attached to a hidden attribute , the hidden attribute can be 0 or 1

What does that mean? That is the sort of species when the priority is to determine whether the species snake, and if so, returns 0 if not, return 1.

Then, to carry out species hidden attribute sort of, after the hidden attribute shoot, then sort the remaining species

In other words, if you can put this statement, seen as a separate column

 

What if we want to snake the tail of the line on the results of it?

Then you can write the FROM PET * the ORDER BY the SELECT IF (Species = 'Snake', 0, 1) DESC , Species;

As some said above, you can put an if statement as a separate column, so you can also add the ASC or DESC sort criteria for him , of course, the default is ASC, can not write.

Thus, the first step is equivalent to the query is a query 0,1 hidden attribute, then DESC sort species = snake because the return value is 0, the reverse order when it is ranked in the final

 

 

So, you are making the hidden attribute prioritization at the same time, for the rest of the sort, you can also additionally be ASC or DESC sort of, not a screenshot.

 

 

Use with IN ORDER BY statement

A single condition above is satisfied, return 0 or 1, and that if you need to use a range of it? You can use the IN statement

Such as the following, I asked the date of birth or the number of rows 1993-02-04 1989-05-13, and the last row

SELECT * FROM pet ORDER BY birth IN('1993-02-04','1989-05-13'),birth;

In this case, the IN statement judge birth, birth if the condition is satisfied, a return is not satisfied, returns 0

Therefore, the condition of the two lines, because the return value is 1, the time for sorting ASC, it is placed in the end.

 

 

========================================================================================

On order by the query access conditions

 适用场景,如表tab_a 有三个字段,

                          如果field1非空则按升序排列,

                               如果field1是空再排field2,

                                       如果 field2非空升序排列,

如果field2是空再排field3,

如果field3非空则升序排列,

如果field3是空。。。。。。。。



例子1    排序boolean类型

CREATE TABLE `tab_b` (
  `field` varchar(255) default NULL,
  `id` int(11) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


INSERT INTO `tab_b` VALUES ('1', 'ture');

INSERT INTO `tab_b` VALUES ('2', 'ture');

INSERT INTO `tab_b` VALUES ('3', 'false');

INSERT INTO `tab_b` VALUES ('4', 'false');


SELECT * from TAB_B ORDER BY  field='true' desc



注意:ORDER  by 后接的字段如果是boolean属性,则false比ture ‘大’!!!!!

                  

########################################################

DROP TABLE IF EXISTS `tab_a`;
CREATE TABLE `tab_a` (
  `id` int(11) NOT NULL,
  `field3` int(11) default NULL,
  `field2` int(11) default NULL,
  `field1` int(11) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


INSERT INTO `tab_a` VALUES ('1', '1', '1', '1');
INSERT INTO `tab_a` VALUES ('2', '2', '2', '2');
INSERT INTO `tab_a` VALUES ('3', '3', '3', null);
INSERT INTO `tab_a` VALUES ('4', '4', null, null);
INSERT INTO `tab_a` VALUES ('5', '5', '5', null);
INSERT INTO `tab_a` VALUES ('6', null, null, null);
INSERT INTO `tab_a` VALUES ('7', null, null, null);
INSERT INTO `tab_a` VALUES ('8', '8', null, null);

SELECT  * FROM TAB_A  ORDER BY
  field1='' desc , field1 asc,
  field2='' desc , field2 asc,
  field3='' desc  ,field3 asc



            此处field1='' 可以看成boolean排序  desc 排序,field1=''为真的排在下面(因为看上面"注意"),否则排在上面(即field1!='');

                  而后面的field1 asc相当于排序field1!=''数据,依次排序field2,field3..........
---------------------
作者:changliangwl
来源:CSDN
原文:https://blog.csdn.net/changliangwl/article/details/42777551
版权声明:本文为博主原创文章,转载请附上博文链接!

Guess you like

Origin www.cnblogs.com/xiaoshen666/p/11004983.html