I want to get distinct values for a field, let say: field1... This needs a query like: "select distin开发者_Python百科ct(field1) from table"
However for some records, field1 is empty and there is another column that is an alternative to field1, which is field2. For the records where field1 is empty I need to use the value of field2. I think I need sort of a conditional select statement with if control something like:
select distinct( (if(field1!='') field1 else field2) ) from table
I have no idea on how to write it. Any help is appreciated...
SELECT DISTINCT IFNULL(field1,field2) FROM table
should do the trick.
SELECT DISTINCT (
IF( coalesce(field1,'') <> '', field1, field2)
)
FROM table
This would work for both null and empty field1
.
i think i did it (not sure if the result is correct):
SELECT DISTINCT (
IF( field1 <> '', field1, field2)
)
FROM table
精彩评论