473,394 Members | 2,100 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Order mixed columns?

Hi all,

I have a MySQL-table. 2 fields.
Field 1 -> Name
Field 2 -> Nick

Now i want to order them alphabetically by Nick,
BUT if Nick is empty it has to pick Name.
How can i mix both of them ?
(example below)

Thanks!

Greetings Frizzle.

-- example --
unordered:
Name | Nick
John | JohnnyBoy
Dean |
George | Jaws
Chris | Ape

result:
Ape
Dean
Jaws
Johnnyboy

Oct 27 '05 #1
4 1586
frizzle (ph********@gmail.com) wrote:
: Hi all,

: I have a MySQL-table. 2 fields.
: Field 1 -> Name
: Field 2 -> Nick

: Now i want to order them alphabetically by Nick,
: BUT if Nick is empty it has to pick Name.
: How can i mix both of them ?
: (example below)
The oracle way would be

select *
from table
order by nvl(nick,name);

nvl returns the first argument unless it's null, in which case it returns
the second argument.

Mysql has a similar function, I think it's called ifnull(), but look it up
to be sure.
--

This programmer available for rent.
Oct 27 '05 #2
Yes, apparently you're right Malcolm.
Thanks for pointing this out for me. Yet 1 question: if a user enters a
Nick,
but decides to remove it later, the field's value isn't actually NULL,
but "" if
i'm right.
What would be the solution for this?

Frizzle.

Oct 27 '05 #3
On 27 Oct 2005 10:41:29 -0700, yf***@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
wrote:
The oracle way would be

select *
from table
order by nvl(nick,name);

nvl returns the first argument unless it's null, in which case it returns
the second argument.


The standard function is COALESCE, which for two arguments is identical to
NVL, but it accepts multiple optional arguments - the first non-null one is the
result.

mysql> select * from example;
+--------+-----------+
| name | nick |
+--------+-----------+
| John | JohnnyBoy |
| Dean | NULL |
| George | Jaws |
| Chris | Ape |
+--------+-----------+
4 rows in set (0.01 sec)

mysql> select coalesce(nick, name)
-> from example
-> order by coalesce(nick, name);
+----------------------+
| coalesce(nick, name) |
+----------------------+
| Ape |
| Dean |
| Jaws |
| JohnnyBoy |
+----------------------+
4 rows in set (0.00 sec)
--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Oct 27 '05 #4
On 27 Oct 2005 10:51:08 -0700, "frizzle" <ph********@gmail.com> wrote:
Yes, apparently you're right Malcolm.
It's customary to quote some context.
Thanks for pointing this out for me. Yet 1 question: if a user enters a
Nick,
but decides to remove it later, the field's value isn't actually NULL,
but "" if
i'm right.
What would be the solution for this?


The correct approach would seem to be to actually set it to NULL instead of ''
if the user blanks it out, since it's a "not applicable" bit of data, rather
than the user's nickname actually being an empty string.

If you're not going to do that then you can work around in the query:

mysql> select * from example;
+--------+-----------+
| name | nick |
+--------+-----------+
| John | JohnnyBoy |
| Dean | NULL |
| George | Jaws |
| Chris | Ape |
+--------+-----------+
4 rows in set (0.01 sec)

mysql> update example set nick='' where nick is null;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from example;
+--------+-----------+
| name | nick |
+--------+-----------+
| John | JohnnyBoy |
| Dean | |
| George | Jaws |
| Chris | Ape |
+--------+-----------+
4 rows in set (0.00 sec)

mysql> select case
-> when nick is null or nick='' then name
-> else nick
-> end nick_or_name
-> from example
-> order by nick_or_name;
+--------------+
| nick_or_name |
+--------------+
| Ape |
| Dean |
| Jaws |
| JohnnyBoy |
+--------------+
4 rows in set (0.01 sec)
--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Oct 27 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Laurence Breeze | last post by:
Is it possible to add a column to a table using the "alter table" statement and specify where in the sequence of columns the new column sits. If not is there any way to alter the order of columns...
9
by: Frederik | last post by:
Hi all, I'm building a C# application that uses a M$ Acces database. In one of the queries I use something like the following: SELECT id FROM mytable WHERE id IN (20, 12, 21, 14) The result...
0
by: Garrek | last post by:
I have an existing ASP.Net application that must be modified to support mixed content: Latin-based languages (i.e. English) intermixed with Arabic. Our code and database assumes everything is...
104
by: Beowulf | last post by:
I have the view below and if I use vwRouteReference as the rowsource for a combo box in an MS Access form or run "SELECT * FROM vwRouteReference" in SQL Query Analyzer, the rows don't come through...
4
by: natG | last post by:
Hi; I am transferring data from MySql to db2 using my own java/jdbc program. Working out ok, except for the fact that our apps use mixed-case names for tables and columns. Although my CREATE TABLE...
2
by: hazz | last post by:
is there a way to have a re-arrangeable list with an asp.net control ? I would like to have a list of database table column values in an ordered list initially loaded by their column order in the...
0
by: todd.freed | last post by:
Hello. I'll make this quick - its quite a conundrum I have the following markup <MyNS:MyControl runat="server" id="myId" CustomProperty1="value" CustomProperty1="value"...
6
by: Opa | last post by:
Hi, I have a DataGrid, whose sourceI am exporting to Excel. This works fine except for the Column ordering. My datasource is not a datatable, with a typical SELECT statement where I can...
4
by: fluff | last post by:
Hello. I want to display some data from a database in a certain order. I have a table with columns A, B, Date. I want to display the records that have data in column A first and I want those...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.