Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

How to use Coalesce in MySQL

A little help here. I really don't understand how to use this coalesce in MySQL

I have read all the pages in page 1 result of how to use coalsece in google result.

I know its meaning that it returns the first non-null value it encounters and null otherwise.

But it's still vague for me.

  1. How come I saw queries that returns multiple values? Isn't it only the first not null value that is returned?
  2. And how do it decide which column to base? coalesce(column1,column2)? what if first column is null and other column is not null?
  3. Or if I'm wrong or my syntax is wrong, how do i properly write it?
  4. Can someone provide a very good and simple example on how to use it?
  5. And when it is desirable to use.
question from:https://stackoverflow.com/questions/17104329/how-to-use-coalesce-in-mysql

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. How come i saw queries that returns multiple values? isnt it only the first not null value that is returned?

    Yes, it is only the first non-NULL value that is returned. You must be mistaken about the queries you have seen where you thought that was not the case: if you could show us an example, we might be able to help clarify the misunderstanding.

  2. And how do it decide which column to base? coalesce(column1,column2)? what if first column is null and other column is not null?

    In order of its arguments: in this example, column1 before column2.

  3. Or if im wrong or my syntax is wrong, how do i properly write it?

    You're not wrong.

  4. Can someone provide a very good and simple example on how to use it?

    Taken from the documentation:

    
    mysql> SELECT COALESCE(NULL,1);
            -> 1
    mysql> SELECT COALESCE(NULL,NULL,NULL);
            -> NULL
    
  5. And when it is desirable to use.

    It is desirable to use whenever one wishes to select the first non-NULL value from a list.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...