English 中文(简体)
如何通过询问撰写复杂命令
原标题:How to write a complex order by query
  • 时间:2011-08-22 09:35:33
  •  标签:
  • mysql

I have a table in MYSQL on which I need to perform a select and then order it by a fairly complex logic. Here is an example of my logic:

if(column_a is not null && column_a > 0){
   var_a = column_a
else
   var_a = 1;

if(column_a is not null){
   var_b = column_b
else
   var_b = 0

if(column_c is not null){
   var_c = column_c
else
   var_c = column_d

页: 1

因此,基本上,我想按照SélectT* 的行文,从表WHERE参数 = 按变量---顺序排列的ORDER。 我将不得不使用一些公式来变换——顺序——但我找不到这样做的任何方法,这不会涉及8个单独的化学文摘社国际协会、欧洲地球科学委员会、以及似乎极其令人迷惑的发言。 任何其他建议?

Thanks!

最佳回答

For a down and dirty way, you can use functions in an order by clause. In specific, you can use the if and ifnull functions. Therefore, you should be able to do something like this:

SELECT ...
ORDER BY IF(some_value IS NOT NULL AND some_value RLIKE  ^[[:digit:]]+$ , some_value, other_value)
       - (column_baz + IF(foo > bar, a, b))/(IFNULL(yet_another_value, 0) + IF(foo > bar, a, b))

(where the regular expression may have to be more complex if you want to include all reals instead of just integers, if you don t want to include a string starting with 0, etc.)

问题回答

a complex case/where structure is really what you need, so the mess is in the syntax. if you want to make the query more elegant, check out the Adding New Functions to MySQL section of the reference manual. then you ll have something like this:

SELECT * from mytable WHERE parameter = value 
ORDER BY meaningfull_name_of_logic(
    foo, bar, some_value, other_value, yet_another_value, column_baz, a, b
);

things:

SELECT my_table.*,
  IF( foo > bar, a, b ) AS var_a,
  IF( some_value IS NOT NULL AND
        is_numeric( some_value ), some_value, other_value ) AS var_b,
  IFNULL( yet_another_value, 0 ) AS var_c
FROM my_table
ORDER BY var_b - (column_baz + var_a)/(var_c + var_a)

There will be 3 extra columns in your results that you should ignore. There are other ways to do this to avoid the 3 extra columns like using variables @var_a := but that will have its own messiness, I prefer the above method.





相关问题
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 ...

热门标签