English 中文(简体)
• 如何使用两条价值观来界定MySQL的秩序,但其中的一条是优先的?
原标题:How to use two values to define order in MySQL but priorize one of them?
  • 时间:2012-05-13 14:12:22
  •  标签:
  • mysql

我有一个讨论委员会,其组织结构如下:

+-------------------------------+
|       TABLE: MESSAGES         |
+-------------------------------+
| id             | int(11)      |
+-------------------------------+
| parent         | int(11)      |
+-------------------------------+
| author         | int(11)      |
+-------------------------------+
| last_reply     | datetime     |
+-------------------------------+
| written        | datetime     |
+-------------------------------+
| title          | varchar(256) |
+-------------------------------+
| content        | text         |
+-------------------------------+

<代码>last_reply的缺省值为NUL。 如果对校正做出答复,该校的所有电文的<代码>last_reply将定在最后答复之日。

What I m trying to achieve is an order of the conversations where both last_reply and written will be considered, but where the last_reply will be priorized over written when available.

So if last_reply is available, we use that value for ordering. If not, we use written.

如果解释不好,我表示歉意。

我迄今最响亮的是:

SELECT *, COALESCE(last_reply,written) AS tmp FROM messages
WHERE parent = 0 ORDER BY written DESC, tmp DESC

这并不正确。

如果你能找到我的话,请指导我正确方向:

感谢。

最佳回答
SELECT * FROM messages
WHERE PARENT=0
ORDER BY IFNULL(last_reply,written) DESC
问题回答

Try this:

SELECT *, COALESCE(last_reply,0) AS with_replay FROM messages
WHERE parent = 0
ORDER BY with_replay DESC,written DESC 




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签