SQL:按其他表选择语句顺序

时间:2014-04-20 13:39:47

标签: mysql sql

我想选择一个列,但顺序不同:

我有2张桌子:

表名:

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

table_temp,id_temp_name id(table_name)的外键:

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

使用此查询:

  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 

我正在寻找与id_temp_name顺序完全相同的结果,但是它显示的值不是由table_temp按顺序排列,因此结果将为:

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

感谢任何建议,。

5 个答案:

答案 0 :(得分:2)

现在,我尝试了很多次,我可以找到显示结果完全相同的查询。但是必须分两步完成工作,并假设id不能为负。

步骤1:创建一个有价值的:

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

步骤2:使用上述变量的嵌套查询:

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)

检查它是否正常工作@ SQL Fiddle

逻辑非常简单,我使用了一些数学运算,并在内部SQL查询中动态添加了新的Id。我在table_name目的中使用了max_id变量两倍于当前id的最大值,因为我生成的id应该比max更大并且按递减顺序(我做了减法)。

试一试!!

答案 1 :(得分:1)

编辑后1:

您可以前往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

编辑2: 好的,然后看下面这会起作用

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

这将为您提供以下结果

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

答案 2 :(得分:1)

如果我理解正确,您需要left outer join,以便将所有记录保存在第一个表中:

  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 ;

order by中的第一个条件是确保缺失值最后,无论第二个子句的顺序如何。

答案 3 :(得分:0)

试试这个:

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;

看看它是否适合你。

答案 4 :(得分:0)

以下是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