简体   繁体   中英

SQL : Select statement order by other table

i want to select a column but with diferent order :

i have 2 table :

table_name:

+------+-----------+
| id   | name      |
+------+-----------+
| 1    |    Sindra |
| 2    |    Auli   |
| 3    |    Brian  |
| 4    |    Bina   |
| 5    |    zian   |
| 6    |    Bri    |
| 7    |    Andre  |
+------+-----------+

table_temp, id_temp_name foreign key of id(table_name) :

+------+--------------+
| id   | id_temp_name |
+------+--------------+
| 1    |    1         |
| 2    |    3         |
| 3    |    4         |
| 4    |    2         |
+------+--------------+

with this query :

  SELECT  table_name.id,table_name.name 
  FROM table_name JOIN table_temp ON table_name.id= table_temp.id_temp_name 
  ORDER BY table_temp.id_temp_name 

i was looking for result that exactly same with id_temp_name order , but it showing value that not in to table_temp to order by asc ,so the result will be :

+------+-----------+
| id   | name      |
+------+-----------+
| 1    |    Sindra |
| 3    |    Brian  |
| 4    |    Bina   |
| 2    |    Auli   |
| 7    |    Andre  |
| 6    |    Bri    |
| 5    |    zian   |
+------+-----------+

thanks for any advice, .

Now, I tried so many times, and I could find queries that displays result exactly same as you needed. But work has to be done in two steps with an assumption that id s can't be negative.

Step-1: Create a valuable:

mysql> SET @max_id = (SELECT MAX(id) FROM table_name) * 2;
Query OK, 0 rows affected (0.00 sec)

Step-2: A nested Query that use above variable:

mysql> SELECT  a.id,
    ->         a.name
    -> FROM (
    -> SELECT  t.id as `id`,
    ->         t.name as `name`, 
    ->         IF(t2.id, t2.id, @max_id := @max_id - 1)  as `id2`
    -> FROM table_name t
    -> LEFT OUTER JOIN table_temp t2
    ->   ON t.id= t2.id_temp_name
    -> GROUP BY t.id
    -> ORDER BY CASE  WHEN t2.id  THEN t2.id
    ->          ELSE  -t.id  END
    -> ) as a
    -> ORDER BY a.id2;
+------+--------+
| id   | name   |
+------+--------+
|    1 | Sindra |
|    3 | Brian  |
|    4 | Bina   |
|    2 | Auli   |
|    7 | Andre  |
|    6 | Bri    |
|    5 | zian   |
+------+--------+
7 rows in set (0.00 sec)

Check it working @ SQL Fiddle .

The logic is very simple, I used a bit math, and added new Id on the fly in inner SQL query. I used max_id variable twice of max value of present id in table_name purpose fully because I am generating id that should be grater than max and in decreasing order (I did subtraction).

Give it a try!!

AFTER EDIT 1:

You can proceed to this_way

SELECT  tgejala.id_gejala,tgejala.nama_gejala 
FROM tgejala 
LEFT JOIN ttemp ON tgejala.id_gejala= ttemp.idtemp_gejala 
ORDER BY CASE WHEN ttemp.id_temp is NULL THEN 1 ELSE 0 END

EDIT 2: Ok then see the below this would work

SELECT  tgejala.id_gejala,tgejala.nama_gejala 
FROM tgejala 
LEFT JOIN ttemp ON tgejala.id_gejala= ttemp.idtemp_gejala 
ORDER BY CASE WHEN ttemp.id_temp is NULL THEN 1 ELSE 0 END, table_name.id desc

This will give you the following result

+------+-----------+
| id   | name      |
+------+-----------+
| 1    |    Sindra |
| 3    |    Brian  |
| 4    |    Bina   |
| 2    |    Auli   |
| 7    |    Andre  |
| 6    |    Bri    |
| 5    |    zian   |
+------+-----------+

If I understand correctly, you need a left outer join so you keep all the records in the first table:

  SELECT  tn.id, tn.name 
  FROM table_name tn LEFT OUTER JOIN
       table_temp tt
       ON tn.id= tt.id_temp_name 
  ORDER BY (tt.id_temp_name is not null) desc,
           tt.id_temp_name ;

The first condition in the order by is to ensure that the missing values go last, regardless of the order of the second clause.

Try This:

Select a.id_temp_name, b.name
from table_temp a, table_name b
where a.id = b.id 
order by a.id_temp_name;

See if that works for you.

Here is the solution for your problem in SQL Server:

SELECT  table_name.id,table_name.name
FROM table_name as table_name
LEFT JOIN table_temp as table_temp ON table_name.id= table_temp.id_temp_name 
ORDER BY CASE 
    WHEN cast(table_temp.id as varchar) IS NOT NULL 
        THEN cast(table_temp.id as varchar) 
    ELSE table_name.name  END asc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM